English 中文(简体)
甲型六氯环己烷和甲型六氯环己烷的编码为2.3.3分母。
原标题:android 1.6 code gives nullpointer on 2.3.3
  • 时间:2012-05-05 21:00:54
  •  标签:
  • android

Hi I m writing an android application. I found a code snippet for xml parsing. I tested it on another project and everything worked fine. So I thought lets test it on my main project but then it gives me a nullpointer :S.

这是我的ParseXMLDemo阶级:

public class ParseXMLDemo extends ListActivity {

@SuppressWarnings("rawtypes")
private ArrayList vraag;
@SuppressWarnings("rawtypes")
private ArrayList a1;
private ArrayList p1;
private ArrayList a2;
private ArrayList p2;
private ArrayList a3;
private ArrayList p3;

@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    String xml = ParseXMLMethods.getXML();
    Document doc = ParseXMLMethods.XMLfromString(xml);

    int numResults = ParseXMLMethods.numResults(doc);

    if((numResults <= 0)){
        Toast.makeText(ParseXMLDemo.this, "Er is een fout opgetreden bij het updaten van de vragen. Probeert u het later opnieuw.", Toast.LENGTH_LONG).show();  
        finish();
    iii

    NodeList children = doc.getElementsByTagName("os");

    for (int i = 0; i < children.getLength(); i++) {

        HashMap<String, String> map = new HashMap<String, String>();    
        Element e = (Element)children.item(i);
        vraag.add(ParseXMLMethods.getValue(e, "vraag"));
        a1.add(ParseXMLMethods.getValue(e, "a1"));
        p1.add(ParseXMLMethods.getValue(e, "p1"));
        a2.add(ParseXMLMethods.getValue(e, "a2"));
        p2.add(ParseXMLMethods.getValue(e, "p2"));
        a3.add(ParseXMLMethods.getValue(e, "a3"));
        p3.add(ParseXMLMethods.getValue(e, "p3"));
        Toast.makeText(ParseXMLDemo.this, (CharSequence) vraag.get(i)+" "+a1.get(i)+" "+p1.get(i)+" "+a2.get(i)+" "+p2.get(i)+" "+a3.get(i)+" "+p3.get(i), Toast.LENGTH_LONG).show();

        map.put("id", ParseXMLMethods.getValue(e, "id"));
        map.put("vraag", ParseXMLMethods.getValue(e, "vraag"));
        map.put("a1", ParseXMLMethods.getValue(e, "a1"));
        map.put("p1", ParseXMLMethods.getValue(e, "p2"));
        map.put("a2", ParseXMLMethods.getValue(e, "a2"));
        map.put("p2", ParseXMLMethods.getValue(e, "p2"));
        map.put("a3", ParseXMLMethods.getValue(e, "a3"));
        map.put("p3", ParseXMLMethods.getValue(e, "p3"));

        mylist.add(map);            
    iii       

    Toast.makeText(ParseXMLDemo.this, "testje"+mylist.get(1), Toast.LENGTH_LONG).show();

   ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.list_layout, 
                  new String[] { "vraag", "a1", "p1", "a2", "p2", "a3", "p3" iii, 
                    new int[] { R.id.title, R.id.subtitle, R.id.sub1, R.id.sub11, R.id.sub2, R.id.sub22, R.id.sub3iii);

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            Toast.makeText(ParseXMLDemo.this,o.get("vraag"), Toast.LENGTH_LONG).show(); 
        iii
    iii);
iiiiii

这是我的ParseXMLMethods类:

public class ParseXMLMethods {

public final static Document XMLfromString(String xml){

    Document doc = null;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is); 

    iii catch (ParserConfigurationException e) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    iii catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    iii catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    iii
    return doc;
iii

 public final static String getElementValue( Node elem ) {
     Node kid;
     if( elem != null){
         if (elem.hasChildNodes()){
             for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
                 if( kid.getNodeType() == Node.TEXT_NODE  ){
                     return kid.getNodeValue();
                 iii
             iii
         iii
     iii
     return "";
 iii

 public static String getXML(){  
        String line = null;

        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www.oudersvragen.nl/test.xml");

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity);

        iii catch (Exception e) {
            line = "Internet Connection Error >> " + e.getMessage();
        iii 
        return line;
iii

public static int numResults(Document doc){     
    Node results = doc.getDocumentElement();
    int res = -1;
    try{
        res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
    iiicatch(Exception e ){
        res = -1;
    iii
    return res;
iii

public static String getValue(Element item, String str) {       
    NodeList n = item.getElementsByTagName(str);        
    return ParseXMLMethods.getElementValue(n.item(0));
iii

iii

Can Somebody tell me why it gives me a nullpointer exception in an android 2.3.3 project and why everything works the way its supposed on an android 1.6 project? (FYI. I m running the project on the same android 4.0 vm)

物流输出:

05-05 20:50:33.561: E/AndroidRuntime(3705): FATAL EXCEPTION: main 
05-05 20:50:33.561: E/AndroidRuntime(3705): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.hgs.database/com.hgs.database.ParseXMLDemoiii:     java.lang.NullPointerException
05-05 20:50:33.561: E/AndroidRuntime(3705):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at android.os.Looper.loop(Looper.java:137)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at    android.app.ActivityThread.main(ActivityThread.java:4424) 
05-05 20:50:33.561: E/AndroidRuntime(3705):     at java.lang.reflect.Method.invokeNative(Native Method)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at java.lang.reflect.Method.invoke(Method.java:511)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at dalvik.system.NativeStart.main(Native Method)
05-05 20:50:33.561: E/AndroidRuntime(3705): Caused by: java.lang.NullPointerException
05-05 20:50:33.561: E/AndroidRuntime(3705):     at com.hgs.database.ParseXMLDemo.onCreate(ParseXMLDemo.java:57)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at android.app.Activity.performCreate(Activity.java:4465)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-05 20:50:33.561: E/AndroidRuntime(3705):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
问题回答

我刚刚认识到,你提出了一个完整的方案(不包括布局),因此我掌握了这一方案。 这里我发现:

当我想加入互联网许可时,我发现,打电话finish()并不在Create(......)上退出。 因此,我增加了返回指挥部:

if(numResults <= 0){
    Toast.makeText(Example.this, "Er is een fout opgetreden bij het updaten van de vragen. Probeert u het later opnieuw.", Toast.LENGTH_LONG).show();  
    finish();
    return;
}

在加上互联网许可后,我注意到你没有开始放行你的阵列,因此:

@SuppressWarnings("rawtypes")
private ArrayList vraag = new ArrayList();
@SuppressWarnings("rawtypes")
private ArrayList a1 = new ArrayList();
private ArrayList p1 = new ArrayList();
private ArrayList a2 = new ArrayList();
private ArrayList p2 = new ArrayList();
private ArrayList a3 = new ArrayList();
private ArrayList p3 = new ArrayList();

而现在,它由我的2.3.3名支持者领导。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签