【问题标题】:java.lang.IllegalStateException for the cw endless adaptercw 无限适配器的 java.lang.IllegalStateException
【发布时间】:2013-03-01 05:03:34
【问题描述】:

首先感谢CommonsWare 提供了这个出色的组件。但我有一个问题让我很伤心。也就是 --> java.lang.IllegalStateException: 适配器的内容发生了变化但是ListView没有收到通知。确保适配器的内容不是从后台线程修改的,而只是从 UI 线程修改的。

以上是我有时会遇到的错误,但有时不会。以下是我的代码。

 protected boolean cacheInBackground() {

                  JSONObject json = null;
                  if(results != null)
                     results.clear();

                  results = new ArrayList<DataSource>();// results is the global variable which is List<DataSource> results; and initialized it here.

                  List<NameValuePair> params = new ArrayList<NameValuePair>();

                   params.add(new BasicNameValuePair("postdata",passeddata));
                   params.add(new BasicNameValuePair("currentpage", (++index)+""));

            json = jParser.makeHttpRequest(urlphp, "POST", params);   // Here is the line that makes http request for paginated results based on index and gets the response.
                  try {
                        int success = json.getInt("success");

                        if (success == 1) {

                            rowcount = json.getInt("numrows");

                            Log.d("row count value is>>>>",""+rowcount); // Here rowcount is the total rows in the database

                            products = json.getJSONArray("products");

                            // looping through All Products
                            for (int i = 0; i < products.length(); i++) {
                                JSONObject c = products.getJSONObject(i);

                              // Storing each json item in variable
                            String id = c.getString(ID);
                            String price = c.getString(PRICE);
                            String name = c.getString(NAME);

                DataSource data = new DataSource(id,name,price);

                            results.add(data);


                        }
                    }else{
                          // product not found

                        successpass = success;
                        message = json.getString("message");

                          return false;

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

                  return(getWrappedAdapter().getCount()<rowcount);


          }


     @Override
      protected void appendCachedData() {
        if (getWrappedAdapter().getCount()<rowcount) {
          @SuppressWarnings("unchecked")
          CustomListAdapter adapter = (CustomListAdapter)getWrappedAdapter();

          //ArrayAdapter<Integer> a=(ArrayAdapter<Integer>)getWrappedAdapter();

            for (DataSource d : results){
                adapter.add(d);
            }

        }else{

        }
      }

正如您在代码中看到的,我将数据存储到 Bean 类对象中,然后将每个对象存储在 List 中。我已经实现了 CustomListView 以在列表中显示详细信息。

但有时会出现上述错误,有时不会。对此进行了大量搜索,发现this 和其他一些一般适配器问题,如this 等,但即使没有点击placeholder 甚至没有调用notifyDataSetChanged(),我也会收到此错误。正如您在我的代码中看到的那样,我从未调用过 notifyDataSetChanged(),但为什么会发生此错误?我的实现编码有问题吗?有人可以帮我解决这个问题吗?由于我将此组件深度集成到我的应用程序中,我现在害怕寻找替代方案。

下面是堆栈跟踪:

03-13 16:12:07.662: E/AndroidRuntime(494): FATAL EXCEPTION: main
03-13 16:12:07.662: E/AndroidRuntime(494): java.lang.IllegalStateException: The content of the adapter has changed but ListView 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 ListView(2130968671, class android.widget.ListView) with Adapter(class com.example.onlinedata.ORAdsList$CustomisedEndlessAdapter)]
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.ListView.layoutChildren(ListView.java:1492)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.AbsListView.onLayout(AbsListView.java:1147)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.View.layout(View.java:7035)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.os.Looper.loop(Looper.java:123)
03-13 16:12:07.662: E/AndroidRuntime(494):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-13 16:12:07.662: E/AndroidRuntime(494):  at java.lang.reflect.Method.invokeNative(Native Method)
03-13 16:12:07.662: E/AndroidRuntime(494):  at java.lang.reflect.Method.invoke(Method.java:521)
03-13 16:12:07.662: E/AndroidRuntime(494):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-13 16:12:07.662: E/AndroidRuntime(494):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-13 16:12:07.662: E/AndroidRuntime(494):  at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 请发布整个堆栈跟踪。
  • @CommonsWare 我从没想过我可以直接感谢你。非常感谢。我用堆栈跟踪更新了我的问题。我想只有你才能解决我的问题。
  • 发生错误的唯一方法是在后台线程上修改适配器的内容。 EndlessAdapter 不应该这样做,这意味着要么你自己在其他地方做,要么EndlessAdapter 中存在错误。为了有希望修复这样的错误,我需要一个可重复的测试用例。如果您碰巧创建了一个可重复的测试用例,请将其(连同我使用它所需遵循的步骤)附在the issue that you opened 上。
  • @CommonsWare 但我在上面粘贴了我的详细代码。如图所示,仅在 appendCachedData() 中通过此行 adapter.add(d); 将数据添加到适配器; :( 我仍然不知道导致该错误的原因。如何制作可重现的测试用例?因为它在我的应用中发生了几次。
  • "如何制作可重现的测试用例?" ——如果我知道的话,我会自己做的。我从来没有遇到过这个错误。因此,我无法修复它,因为即使我随机更改代码,我也不知道这些随机更改是否真的有帮助。

标签: android listview commonsware-cwac


【解决方案1】:

终于有人给了我一个可重现的测试用例,这个错误现在已经从 v1.2.1 修复了。获取一个新的 repo 副本,或者一个新的 JAR。在 v1.2.0 上不需要更改代码。

【讨论】:

  • 这就是我测试时导致崩溃的原因。但我想我是随机得到的。无论如何,将包括新的罐子并测试它。非常感谢您回帖。
猜你喜欢
  • 1970-01-01
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多