Android java.lang.IllegalStateException:适配器的内容已更改,但ExtendableListView未收到通知

时间:2015-10-09 07:13:12

标签: android illegalstateexception

我在单个活动中使用两个listview。当我点击第一个按钮时,listview1被填充。当我点击第二个按钮时,listview2被填满。但是之间发生了一些错误。我没有得到什么问题。

Stacktrace是,

FATAL EXCEPTION: main
Process: com.icanstudioz.izabuy, PID: 18665
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(2131689644, class com.etsy.android.grid.StaggeredGridView) with Adapter(class com.icanstudioz.izabuy.adapter.FavoritesAdapter)]
        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:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:562)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2323)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
        at android.view.Choreographer.doCallbacks(Choreographer.java:591)
        at android.view.Choreographer.doFrame(Choreographer.java:560)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
        at dalvik.system.NativeStart.main(Native Method)

代码是,对于第一个lisview,

public void getFavorite() throws JSONException {
    IzabuyServer.get("h2app/api/product/data_list/", params,
            new JsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers,
                                      JSONArray response) {
                    super.onSuccess(statusCode, headers, response);
                    try {
                        everyList.clear();
                        GsonBuilder gsonBuilder = new GsonBuilder();
                        gsonBuilder.registerTypeAdapter(Favorites.class,
                                new Favorites());

                        Gson gson = gsonBuilder.create();
                        everyList = gson.fromJson(response.toString(),
                                new TypeToken<List<Favorites>>() {
                                }.getType());

                        if (response.length() <= 0) {
                            tv = (TextView) findViewById(R.id.no_text);
                            tv.setVisibility(View.VISIBLE);
                            list.setVisibility(View.GONE);
                            mSwipeRefreshLayout.setEnabled(false);
                            mSwipeRefreshLayout2.setEnabled(false);
                            if (db.getString("type").equalsIgnoreCase("2")) {
                                tv.setText(getResources().getString(R.string.fav_empty));
                            } else if (db.getString("type").equalsIgnoreCase("3")) {
                                tv.setText(getResources().getString(R.string.rec_empty));
                            }
                        } else {
                            tv = (TextView) findViewById(R.id.no_text);
                            list2.setVisibility(View.GONE);
                            list.setVisibility(View.VISIBLE);
                            tv.setVisibility(View.GONE);
                            mSwipeRefreshLayout.setEnabled(true);
                            mSwipeRefreshLayout2.setEnabled(false);
                            adapterFavorite = new FavoritesAdapter(MainScreenActivity.this,
                                    everyList);
                            list.setAdapter(adapterFavorite);
                            list.requestLayout();
                            list.setSelection(0);
                            list.setColumnCount(3);
                            adapterFavorite.notifyDataSetChanged();

                        }

                        if (pDialog != null) {
                            pDialog.dismiss();
                        }


                        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view,
                                                    int position, long id) {
                                Intent i = new Intent(MainScreenActivity.this, ItemDetailActivity.class);
                                i.putExtra("product_id", everyList.get(position).getId());
                                startActivity(i);
                            }
                        });
                        mSwipeRefreshLayout.setRefreshing(false);

                    } catch (NullPointerException e) {
                        e.printStackTrace();
                        mSwipeRefreshLayout.setRefreshing(false);
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                        mSwipeRefreshLayout.setRefreshing(false);
                    }

                    if (pDialog != null) {
                        pDialog.dismiss();
                    }

                }
            });
}

代码是,对于第二个lisview,

public void getSponsor() throws JSONException {
    IzabuyServer.get("h2app/api/product/data_list/", params,
            new JsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers,
                                      JSONArray response) {
                    super.onSuccess(statusCode, headers, response);

                    Log.e("sponsor", response.toString());

                    try {
                        sponserList.clear();
                        everyList.clear();
                        GsonBuilder gsonBuilder = new GsonBuilder();
                        gsonBuilder.registerTypeAdapter(Sponsors.class,
                                new Sponsors());

                        Gson gson = gsonBuilder.create();
                        sponserList = gson.fromJson(response.toString(),
                                new TypeToken<List<Sponsors>>() {
                                }.getType());

                        if (sponserList.size() <= 0) {
                            tv = (TextView) findViewById(R.id.no_text);
                            tv.setVisibility(View.VISIBLE);
                            list2.setVisibility(View.GONE);
                            list.setVisibility(View.GONE);
                            mSwipeRefreshLayout.setEnabled(false);
                            mSwipeRefreshLayout2.setEnabled(false);
                            tv.setText(getResources().getString(R.string.spo_empty));
                        } else {
                            list2.setVisibility(View.VISIBLE);
                            list.setVisibility(View.GONE);
                            tv = (TextView) findViewById(R.id.no_text);
                            tv.setVisibility(View.GONE);
                            mSwipeRefreshLayout.setEnabled(false);

                            sponserAdapter = new SponsorsAdapter(MainScreenActivity.this, sponserList);
                            list2.setAdapter(sponserAdapter);
                            list2.requestLayout();
                            list2.smoothScrollToPosition(0);
                            sponserAdapter.notifyDataSetChanged();


                        }


                        list2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent,
                                                    View view, int position, long id) {
                                Intent i = new Intent(MainScreenActivity.this,
                                        SponsorDetailActivity.class);
                                Bundle b = new Bundle();
                                b.putString("user_id", sponserList.get(position)
                                        .getUser_id());
                                b.putString("name", sponserList.get(position).getName());
                                i.putExtras(b);
                                startActivity(i);
                            }
                        });


                        mSwipeRefreshLayout2.setRefreshing(false);
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                        mSwipeRefreshLayout2.setRefreshing(false);
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    }

                    if (pDialog != null) {
                        pDialog.dismiss();
                    }


                }
            });
}

请帮我解决这个问题。谢谢。

0 个答案:

没有答案