1

XMLパーサーを使用してWebサイトからいくつかのものを抽出しており、Asynctask内にコードがあります。何らかの理由でNullPointerExceptionが発生し続けるのはなぜですか?

エラーの原因と、それをどのように修正しますか? お時間をいただきありがとうございます。

これはLogCatです

02-08 23:34:08.835: E/AndroidRuntime(28103): FATAL EXCEPTION: main
02-08 23:34:08.835: E/AndroidRuntime(28103): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thenewlistivew/com.thenewlistivew.MainActivity}: java.lang.NullPointerException
02-08 23:34:08.835: E/AndroidRuntime(28103):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at android.os.Looper.loop(Looper.java:137)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at android.app.ActivityThread.main(ActivityThread.java:4511)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at java.lang.reflect.Method.invokeNative(Native Method)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at java.lang.reflect.Method.invoke(Method.java:511)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at dalvik.system.NativeStart.main(Native Method)
02-08 23:34:08.835: E/AndroidRuntime(28103): Caused by: java.lang.NullPointerException
02-08 23:34:08.835: E/AndroidRuntime(28103):    at com.thenewlistivew.MainActivity.onCreate(MainActivity.java:40)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at android.app.Activity.performCreate(Activity.java:4486)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
02-08 23:34:08.835: E/AndroidRuntime(28103):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
02-08 23:34:08.835: E/AndroidRuntime(28103):    ... 11 more

私のAsyncTaskコード:

private class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void> {
        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

        @Override
        protected void onPreExecute() {
            // update the UI immediately after the task is executed
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {

            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML from URL
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_SONG);
            // looping through all song nodes <song>
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                // adding HashList to ArrayList
                songsList.add(map);
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);

        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

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

            // Getting adapter by passing xml data ArrayList
            adapter = new LazyAdapter(MainActivity.this, songsList);
            list.setAdapter(adapter);
        }
    }

OnCreateメソッドコード

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

        new ShowDialogAsyncTask().execute();

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            }
        });
    }
4

1 に答える 1

1

あなたが持っている -

list.setOnItemClickListener(new OnItemClickListener()

しかし、初期化することはありませんlist

この行をあなたのonCreate()-に移動します

list = (ListView) findViewById(R.id.list);
于 2013-02-09T05:01:39.823 に答える