0

我是一名新的 Android 程序员,最近,我在列表视图中使用拉解析器解析 xml 文件。在 listview 中解析时,我得到了这个异常。

09-13 05:37:00.401: E/AndroidRuntime(1167): FATAL EXCEPTION: main
09-13 05:37:00.401: E/AndroidRuntime(1167): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplexmlpullapp/com.example.simplexmlpullapp.MainActivity}: java.lang.NullPointerException

那么下一步该怎么做呢?这样这个异常就会被删除。我的代码是:

MainActivity.java

 package com.example.simplexmlpullapp;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;


import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    ListView list;
    String str;
    ArrayList<Data> arrdata;
    ArrayList<String> arrayofString, parcedCountry;
    String textforinput = "<foo>Hello World!</foo>";
    String parsedData = "";
    XmlPullParser xmlPullParser;
    ArrayAdapter<String>adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list=(ListView)findViewById(R.id.li);

        adapter=new ArrayAdapter<String>(MainActivity.this, 
                android.R.layout.simple_list_item_1,android.R.id.text1,arrayofString);
        list.setAdapter(adapter);
         try
         {
                 InputStream is;
                 is=getAssets().open("countries.xml");
                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
                 StringBuilder total = new StringBuilder();
                 String line;
                 while ((line = r.readLine()) != null)
                 {
                         total.append(line);
                 }

                 //Log.e("File",""+total.toString()); 
                 parseCountries(total.toString());
         }
         catch (Exception e) 
         {
                 // TODO: handle exception
         }
}

private void parseCountries(String string)
{

        int eventType=0;
        String countries = null;
        boolean countryFlag=false;
        ArrayList<String>parsedCountry=new ArrayList<String>();
        try 
        {
                xmlPullParser=XmlPullParserFactory.newInstance().newPullParser();
                xmlPullParser.setInput(new StringReader(string));
                eventType = xmlPullParser.getEventType();
        }
        catch (XmlPullParserException e) {
                e.printStackTrace();
        }

        while(eventType!=XmlPullParser.END_DOCUMENT)
        {
                switch (eventType) 
                {
                case XmlPullParser.START_DOCUMENT:
                        break;

                case XmlPullParser.START_TAG:
                        if(xmlPullParser.getName().equalsIgnoreCase("country"))
                        {

                                countryFlag=true;        
                        }
                        break;
                case XmlPullParser.TEXT:

                        if(countryFlag)
                        {
                                countries=xmlPullParser.getText().toString().trim();
                                Log.i("parse_example", "country"+countries); 
                        }        
                        break;

                case XmlPullParser.END_TAG:
                        if(xmlPullParser.getName().equalsIgnoreCase("country"))
                        {
                                parsedCountry.add(countries);
                                countryFlag=false;        
                        }
                        break;
                        //end of switch        
                }

                try {
                    eventType=xmlPullParser.next();

                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }

//        arrayofString=new ArrayList<String>();
//        for(int i=0;i<arrdata.size();i++)
//        {
//            Data data=arrdata.get(i);
//            arrayofString.add(data.getCountry());
//            Log.i("parse_example", "country"+countries);
//        }

}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

MainActivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lodge Names"
        tools:context=".MainActivity" />

    <ListView
        android:id="@+id/li"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#000000" >
    </ListView>

</LinearLayout>
4

2 回答 2

6

请在 onCreate() 中执行此操作;

在创建适配器之前初始化

arrayofString = new ArrayList<String>();
parcedCountry = new ArrayList<String>();

更新显示列表

adapter=new ArrayAdapter<String>(MainActivity.this, 
                android.R.layout.simple_list_item_1,android.R.id.text1,parcedCountry); //UPDATE HERE
        list.setAdapter(adapter);
         try
         {
                 InputStream is;
                 is=getAssets().open("countries.xml");
                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
                 StringBuilder total = new StringBuilder();
                 String line;
                 while ((line = r.readLine()) != null)
                 {
                         total.append(line);
                 }

                 //Log.e("File",""+total.toString()); 

                 parseCountries(total.toString());

                 adapter.notifyDataSetChanged(); //UPDATE HERE

         }
         catch (Exception e) 
         {
                 // TODO: handle exception
         }
于 2013-09-13T05:45:11.947 回答
0

尝试这个..

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = (ListView) findViewById(R.id.li);
            arrayofString = new ArrayList<String>();
            parcedCountry = new ArrayList<String>();
    try {
        InputStream is;
        is = getAssets().open("countries.xml");
        BufferedReader r = new BufferedReader(new InputStreamReader(is));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line);
        }

        // Log.e("File",""+total.toString());
        parseCountries(total.toString());
        adapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1, android.R.id.text1,
                arrayofString);
        list.setAdapter(adapter);
    } catch (Exception e) {
        e.printStackTrace();
        // TODO: handle exception
    }
}
于 2013-09-13T05:48:20.853 回答