Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android cw适配器的java.lang.IllegalStateException_Android_Listview_Commonsware Cwac - Fatal编程技术网

Android cw适配器的java.lang.IllegalStateException

Android cw适配器的java.lang.IllegalStateException,android,listview,commonsware-cwac,Android,Listview,Commonsware Cwac,首先,感谢Commonware提供了这个伟大的组件。但我有一个问题,这一直让我伤心一些时候。也就是-->java.lang.IllegalStateException:适配器的内容已更改,但ListView未收到通知。确保适配器的内容不是从后台线程修改的,而是从UI线程修改的。 以上是我有时会遇到的错误,但有时不是。下面是我的代码 protected boolean cacheInBackground() { JSONObject json = null;

首先,感谢
Commonware
提供了这个伟大的组件。但我有一个问题,这一直让我伤心一些时候。也就是-->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{

        }
      }

终于有人给了我一个可复制的测试用例,这个bug从v1.2.1开始就被修复了。拿一份新的回购协议,或者一个新的罐子。v1.2.0版不需要更改代码。

请发布整个堆栈跟踪。@commonware我从未想过我可以直接感谢您。非常感谢你。我用堆栈跟踪更新了我的问题。我认为只有你能解决我的问题。发生错误的唯一方法是在后台线程上修改适配器的内容
EndlessAdapter
不应该这样做,这意味着要么你自己在其他地方做,要么
EndlessAdapter
中有一个bug。为了有希望修复这样一个bug,我需要一个可重复的测试用例。如果您碰巧创建了一个可重复的测试用例,请将它(以及使用它所需的步骤)附加到@commonware上,但我在上面粘贴了详细的代码。如图所示,数据仅通过此行adapter添加到appendCachedData()中的适配器中。add(d);:(我仍然不知道是什么导致了这个错误。如何制作一个可复制的测试用例?因为它在我的应用程序中发生过几次。“如何制作一个可复制的测试用例?”--如果我知道的话,我会自己做的。我从来没有遇到过这个错误。因此,我没有办法修复它,因为即使我随机更改代码,我也不知道这些随机更改是否真的有用。这是导致崩溃的原因吗?即使在我测试的时候。但我想我是随机得到的。无论如何,我要包括新的jar并测试它。Th我非常感谢你的回帖。
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)