java.lang.IllegalStateException:适配器的内容已更改

时间:2015-03-23 05:08:04

标签: android android-asynctask illegalstateexception

 java.lang.IllegalStateException: The content of the adapter has changed but ExtendableListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.

点击&#34时,我偶尔会遇到异常;加载更多"动态列表视图中的选项。 我在notifyDataSetChanged()内使用postExecute()。什么可以解决这个问题以及如何阻止应用程序崩溃?

adapter = new GHomeGridAdapter(getActivity().getApplicationContext(), R.layout.g_grid, ggridList);

gridview.setAdapter(adapter);
new GetData().execute(limit); 

loadMore.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        loadMore.setVisibility(View.GONE);
        new GetData().execute(limit);
    }
});

gridview.setOnScrollListener(new AbsListView.OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

        if (gridview.getLastVisiblePosition() + 1 == limit) {
            loadMore.setVisibility(View.VISIBLE);
        }
    }

private class GetData extends AsyncTask<Integer, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(myContext);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Integer... arg0) {
        GyHandler sh = new GyHandler();
        int offest=arg0[0];
        limit=arg0[0]+30;
        String urlgifs="xyz";
        String jsonStr = sh.makeServiceCall(urlgifs, GHandler.GET);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
        }
        return null;
    }

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

        if (pDialog.isShowing())
            pDialog.dismiss();

        adapter.notifyDataSetChanged();
    }

}

日志 -

 java.lang.IllegalStateException: The content of the adapter has changed but ExtendableListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ExtendableListView(2131427542, class com.etsy.android.grid.StaggeredGridView) with Adapter(class xyz.GiphyHomeGridAdapter)]
            at com.etsy.android.grid.ExtendableListView.layoutChildren(ExtendableListView.java:559)
            at com.etsy.android.grid.StaggeredGridView.layoutChildren(StaggeredGridView.java:358)
            at com.etsy.android.grid.ExtendableListView.onLayout(ExtendableListView.java:513)
            at android.view.View.layout(View.java:14937)
            at android.view.ViewGroup.layout(ViewGroup.java:4651)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
            at android.view.View.layout(View.java:14937)
            at android.view.ViewGroup.layout(ViewGroup.java:4651)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14937)
            at android.view.ViewGroup.layout(ViewGroup.java:4651)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:887)
            at android.view.View.layout(View.java:14937)
            at android.view.ViewGroup.layout(ViewGroup.java:4651)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14937)
            at android.view.ViewGroup.layout(ViewGroup.java:4651)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
            at android.view.View.layout(View.java:14937)
            at android.view.ViewGroup.layout(ViewGroup.java:4651)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14937)
            at android.view.ViewGroup.layout(ViewGroup.java:4651)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14937)
            at android.view.ViewGroup.layout(ViewGroup.java:4651)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14937)
            at android.view.ViewGroup.layout(ViewGroup.java:4651)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2132)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1872)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1075)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5878)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)

1 个答案:

答案 0 :(得分:-1)

尝试这样做以确保主线程中发生notify

       @Override
        protected void onPreExecute() {
         super.onPreExecute();
         runOnUiThread(new Runnable() {
               @Override
               public void run() {
                 pDialog = new ProgressDialog(myContext);
                 pDialog.setMessage("Please wait...");
                 pDialog.setCancelable(false);
                 pDialog.show();
               }
             });
         }

        @Override
        protected void onPostExecute(Void result)
        {
         super.onPostExecute(result);
         if (pDialog.isShowing())
             runOnUiThread(new Runnable() {

               @Override
               public void run() {
                pDialog.dismiss();
                adapter.notifyDataSetChanged();
               }
             });

        }