gpt4 book ai didi

android - cw 无限适配器的 java.lang.IllegalStateException

转载 作者:行者123 更新时间:2023-11-29 18:02:08 25 4
gpt4 key购买 nike

首先要感谢 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 类对象中,然后将每个对象存储到列表中。我已经实现了 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)

最佳答案

终于有人给了我一个可重现的测试用例,这个错误现在从 v1.2.1 开始修复了。获取 repo 的新副本或新的 JAR。 v1.2.0 之后无需更改代码。

关于android - cw 无限适配器的 java.lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15382315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com