更新listView时更改片段时出错

时间:2014-10-25 07:34:46

标签: android listview android-fragments android-listview

我的应用程序包含3个选项卡,其中一个选项卡用另一个片段切换其片段。这两个片段都包含listviews。我的问题是,如果我在列表视图加载来自互联网的数据时使用后退按钮(使用asynctask),应用程序崩溃。

加载第二个片段时执行的函数:

void load(final String q) {

    class HttpGetAsyncTask extends AsyncTask<String, Void, List<String>> {

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

        @Override
        protected List<String> doInBackground(String... params) {

            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet("url");
            String v = null, c = null;

            try {
                HttpResponse httpResponse = httpClient.execute(httpGet);
                InputStream inputStream = httpResponse.getEntity()
                        .getContent();
                InputStreamReader inputStreamReader = new InputStreamReader(
                        inputStream);
                BufferedReader bufferedReader = new BufferedReader(
                        inputStreamReader);
                StringBuilder stringBuilder = new StringBuilder();
                String bufferedStrChunk = null;

                while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
                    stringBuilder.append(bufferedStrChunk);
                }
                String js = stringBuilder.toString();

                try {
                        JSONObject jsonObj = new JSONObject(js);
                        String[] n = new String[17];
                        for(int i = 6;i<=16;i++){
                            n[i] = jsonObj.getString("" + i);
                        }

                        for(int i=6;i<=16;i++){
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put("r", label_eq[i]);
                            map.put("c", n[i]);
                            fillMaps.add(map);
                        }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } catch (ClientProtocolException cpe) {
                cpe.printStackTrace();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            LIST.add(v);
            LIST.add(c);
            return LIST;
        }

        @Override
        protected void onPostExecute(List<String> result) {
            super.onPostExecute(result);

            SimpleAdapter a = new SimpleAdapter(getActivity(), fillMaps,R.layout.my_list_item , from, to); //this is the line 158th line
            l.setAdapter(a);    
        }


    }

    HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask();
    httpGetAsyncTask.execute();
}

LogCat错误:

10-25 12:43:34.423: E/AndroidRuntime(779): FATAL EXCEPTION: main
10-25 12:43:34.423: E/AndroidRuntime(779): java.lang.NullPointerException
10-25 12:43:34.423: E/AndroidRuntime(779):  at android.widget.SimpleAdapter.<init>(SimpleAdapter.java:85)
10-25 12:43:34.423: E/AndroidRuntime(779):  at yash.proj.stocks.eq$1HttpGetAsyncTask.onPostExecute(eq.java:158)
10-25 12:43:34.423: E/AndroidRuntime(779):  at yash.proj.stocks.eq$1HttpGetAsyncTask.onPostExecute(eq.java:1)
10-25 12:43:34.423: E/AndroidRuntime(779):  at android.os.AsyncTask.finish(AsyncTask.java:631)
10-25 12:43:34.423: E/AndroidRuntime(779):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-25 12:43:34.423: E/AndroidRuntime(779):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-25 12:43:34.423: E/AndroidRuntime(779):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 12:43:34.423: E/AndroidRuntime(779):  at android.os.Looper.loop(Looper.java:137)
10-25 12:43:34.423: E/AndroidRuntime(779):  at android.app.ActivityThread.main(ActivityThread.java:5041)
10-25 12:43:34.423: E/AndroidRuntime(779):  at java.lang.reflect.Method.invokeNative(Native Method)
10-25 12:43:34.423: E/AndroidRuntime(779):  at java.lang.reflect.Method.invoke(Method.java:511)
10-25 12:43:34.423: E/AndroidRuntime(779):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-25 12:43:34.423: E/AndroidRuntime(779):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-25 12:43:34.423: E/AndroidRuntime(779):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我无法发表评论。

  

由于onDestroyView()不一定每次都被调用,我应该在onPause()上取消它

你是对的,我会假设你正在这样做。

如果您已覆盖onBackPressed()方法,请告知我们。假设你还没有:

取消后,您需要修改AsyncTask的onPostExecute(..)方法。将方法替换为以下内容:

 @Override
    protected void onPostExecute(List<String> result) {
        super.onPostExecute(result);
        if(isCancelled() || !isAdded()) {
            //Fragment is not in a state to meaningfully use the Main thread
            return;
        }
        SimpleAdapter a = new SimpleAdapter(getActivity(), fillMaps,R.layout.my_list_item , from, to); //this is the line 158th line
        l.setAdapter(a);    
    }

isAdded()方法是确保按照HERE

将片段添加到活动中

或者,我认为也可以使用isVisible()

以上内容应解决您的问题。如果您仍然遇到问题,请发布您的Fragments的父活动和logcat。