Xml解析Null异常

时间:2013-06-18 06:31:09

标签: android xml

我正在尝试构建一个简单的xml解析应用程序。但不幸的是,运行代码时似乎出现了错误。有人可以帮助我吗?感谢

MainActivity.java

public class MainActivity extends Activity {
    Button button;
    ListView listView;
    List<WorldPopulation> employees = null;
    ProgressDialog mProgressDialog;

    static final String RANK = "rank";
    static final String COUNTRY = "country";
    static final String POPULATION = "population";

    ListView listview;
    ArrayAdapter<WorldPopulation> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        new DownloadXML().execute();
    }

    private class DownloadXML extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            mProgressDialog = new ProgressDialog(MainActivity.this);

            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);

            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            employees = new ArrayList<WorldPopulation>();

            XMLParser parser = new XMLParser();

            String xml = parser
                    .getXmlFromUrl("My Secret URL");

            Document doc = parser.getDomElement(xml);
            try {
                NodeList nl = doc.getElementsByTagName("ranking");
                for (int i = 0; i < nl.getLength(); i++) {
                    Element e = (Element) nl.item(i);
                    WorldPopulation employee = new WorldPopulation();
                    employee.setRank(parser.getValue(e, RANK));
                    employee.setCountry(parser.getValue(e, COUNTRY));
                    employee.setPopulation(parser.getValue(e, POPULATION));

                    employees.add(employee);
                }
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {


            listview = (ListView) findViewById(R.id.listview);
            adapter = new ArrayAdapter<WorldPopulation>(MainActivity.this,
                    R.layout.list_item, employees);
            listView.setAdapter(adapter);
            mProgressDialog.dismiss();
        }
    }

}

Worldpopulation.class

public class WorldPopulation {
    private String rank;
    private String country;
    private String population;

    public String getRank() {
        return rank;
    }


    public void setRank(String rank) {
        this.rank = rank;
    }


    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getPopulation() {
        return population;
    }


    public void setPopulation(String population) {
        this.population = population;
    }

}

Logcat:

06-18 14:26:16.377: E/AndroidRuntime(22886): FATAL EXCEPTION: main
06-18 14:26:16.377: E/AndroidRuntime(22886): java.lang.NullPointerException
06-18 14:26:16.377: E/AndroidRuntime(22886):    at com.example.exampleparse.MainActivity$DownloadXML.onPostExecute(MainActivity.java:89)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at com.example.exampleparse.MainActivity$DownloadXML.onPostExecute(MainActivity.java:1)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at android.os.AsyncTask.finish(AsyncTask.java:602)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at android.os.Looper.loop(Looper.java:137)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at android.app.ActivityThread.main(ActivityThread.java:4507)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at java.lang.reflect.Method.invoke(Method.java:511)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
06-18 14:26:16.377: E/AndroidRuntime(22886):    at dalvik.system.NativeStart.main(Native Method)

修改

public class MainActivity extends Activity {
    Button button;
    ListView listView;
    List<WorldPopulation> employees = null;

    ProgressDialog mProgressDialog;

    static final String RANK = "rank";
    static final String COUNTRY = "country";
    static final String POPULATION = "population";

    ListView listview;
    ArrayAdapter<WorldPopulation> adapter;
    private Context context;

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

        new DownloadXML().execute();
    }
    public void DownloadXML(Context context) {
        this.context = context;
    }
    private class DownloadXML extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            mProgressDialog = new ProgressDialog(MainActivity.this);

            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);

            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            employees = new ArrayList<WorldPopulation>();

            XMLParser parser = new XMLParser();

            String xml = parser
                    .getXmlFromUrl("MY Secret URL");

            Document doc = parser.getDomElement(xml);
            try {
                NodeList nl = doc.getElementsByTagName("ranking");
                for (int i = 0; i < nl.getLength(); i++) {
                    Element e = (Element) nl.item(i);
                    WorldPopulation employee = new WorldPopulation();
                    employee.setRank(parser.getValue(e, RANK));
                    employee.setCountry(parser.getValue(e, COUNTRY));
                    employee.setPopulation(parser.getValue(e, POPULATION));

                    employees.add(employee);
                }
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {

            listview = (ListView) ((Activity) context).findViewById(R.id.listview);

            adapter = new ArrayAdapter<WorldPopulation>(MainActivity.this,
                    R.layout.list_item, employees);
            listView.setAdapter(adapter);
            mProgressDialog.dismiss();
        }
    }

}

logcat的:

06-18 15:29:47.267: E/AndroidRuntime(31737): FATAL EXCEPTION: main
06-18 15:29:47.267: E/AndroidRuntime(31737): java.lang.NullPointerException
06-18 15:29:47.267: E/AndroidRuntime(31737):    at com.example.exampleparse.MainActivity$DownloadXML.onPostExecute(MainActivity.java:92)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at com.example.exampleparse.MainActivity$DownloadXML.onPostExecute(MainActivity.java:1)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at android.os.AsyncTask.finish(AsyncTask.java:602)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at android.os.Looper.loop(Looper.java:137)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at android.app.ActivityThread.main(ActivityThread.java:4507)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at java.lang.reflect.Method.invoke(Method.java:511)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
06-18 15:29:47.267: E/AndroidRuntime(31737):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

问题是你必须从你的上下文中调用findViewById,否则它将找不到View。

尝试更改onPostExecute()中的行,如下所示:

listview = (ListView) MainActivity.this.findViewById(R.id.listview);

我建议为需要Context的AsyncTask实现一个构造函数,并将其保存到私有字段。

public DownloadXML(Context context) {
    this.context = context;
}

然后你可以简单地做

listview = (ListView) context.findViewById(R.id.listview);

在您的Activity中创建AsyncTask,如下所示:

new DownloadXML(MainActivity.this).execute();

这是一种方法。您需要实现构造函数。这是解决方案:

public class MainActivity extends Activity {
Button button;
ListView listView;
List<WorldPopulation> employees = null;

ProgressDialog mProgressDialog;

static final String RANK = "rank";
static final String COUNTRY = "country";
static final String POPULATION = "population";

ListView listview;
ArrayAdapter<WorldPopulation> adapter;
private Context context;

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

    new DownloadXML(MainActivity.this).execute();
}

private class DownloadXML extends AsyncTask<Void, Void, Void> {

    private Context context;

    public void DownloadXML(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        mProgressDialog = new ProgressDialog(MainActivity.this);

        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);

        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        employees = new ArrayList<WorldPopulation>();

        XMLParser parser = new XMLParser();

        String xml = parser
                .getXmlFromUrl("MY Secret URL");

        Document doc = parser.getDomElement(xml);
        try {
            NodeList nl = doc.getElementsByTagName("ranking");
            for (int i = 0; i < nl.getLength(); i++) {
                Element e = (Element) nl.item(i);
                WorldPopulation employee = new WorldPopulation();
                employee.setRank(parser.getValue(e, RANK));
                employee.setCountry(parser.getValue(e, COUNTRY));
                employee.setPopulation(parser.getValue(e, POPULATION));

                employees.add(employee);
            }
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {

        listview = (ListView) ((Activity) context).findViewById(R.id.listview);

        adapter = new ArrayAdapter<WorldPopulation>(MainActivity.this,
                R.layout.list_item, employees);
        listView.setAdapter(adapter);
        mProgressDialog.dismiss();
    }
}
}