可展开的listview asynchrone任务空指针异常

时间:2014-12-23 20:10:59

标签: android asynchronous expandablelistview

早上好 我试图解析xml文件并在可扩展列表视图中显示结果,所以我使用了一个名为prepareListData()的方法来完成这项工作,我在asynchrone任务中调用它,但每次我都得到空指针异常。

这个代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mofakra);
        zone = (EditText) findViewById(R.id.research);
        //maListViewPerso = (ListView) findViewById(R.id.listView2);
        maList = (ExpandableListView) findViewById(R.id.listexp);
        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {


            protected void onPreExecute() {
                mProgressDialog = new ProgressDialog(Mofakra.this, ProgressDialog.STYLE_SPINNER);
                mProgressDialog.show();
            }

            protected Void doInBackground(Void... param) {
                try
                {


                try {
                    prepareListData();
                } catch (XPathExpressionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } //appel de la méthode en interne
                Thread.sleep(10000);


                }

                 catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }

            protected void onPostExecute(Void result) {
                mProgressDialog.dismiss(); 
            }
        };
        task.execute((Void[])null); 
        addKeyListener();
        listAdapter = new ExpandableListAdapter(getApplicationContext(), listDataHeader, listDataChild);
            // setting list adapter
        maList.setAdapter(listAdapter);
..............

这是执行中的跟踪日志;

12-23 19:59:30.252: E/AndroidRuntime(21463): FATAL EXCEPTION: main
12-23 19:59:30.252: E/AndroidRuntime(21463): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.albir/com.example.albir.Mofakra}: java.lang.NullPointerException
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.os.Looper.loop(Looper.java:137)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.app.ActivityThread.main(ActivityThread.java:4745)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at java.lang.reflect.Method.invokeNative(Native Method)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at java.lang.reflect.Method.invoke(Method.java:511)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at dalvik.system.NativeStart.main(Native Method)
12-23 19:59:30.252: E/AndroidRuntime(21463): Caused by: java.lang.NullPointerException
12-23 19:59:30.252: E/AndroidRuntime(21463):    at com.example.albir.ExpandableListAdapter.getGroupCount(ExpandableListAdapter.java:71)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.widget.ExpandableListConnector.getCount(ExpandableListConnector.java:397)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.widget.ListView.setAdapter(ListView.java:460)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:470)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at com.example.albir.Mofakra.onCreate(Mofakra.java:99)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.app.Activity.performCreate(Activity.java:5008)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-23 19:59:30.252: E/AndroidRuntime(21463):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-23 19:59:30.252: E/AndroidRuntime(21463):    ... 11 more

我之前没有使用asynctask测试过该方法,但它确实有效,但需要一些时间才能决定使用progressdialog并在单独的线程中执行此方法。 我希望你帮助我。

0 个答案:

没有答案