gpt4 book ai didi

android - 为什么执行 listview.setAdapter(_adapter) 两次会使应用程序崩溃?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:04:08 25 4
gpt4 key购买 nike

我遇到的关键问题是 lv_apps.setAdapter(_adapter) TWICE 导致我的应用崩溃。(基本上,当我第二次调用 populateListView_trial() 时,应用程序崩溃了——违规行是 lv_apps.setAdapter(_adapter)

堆栈跟踪在函数下面

private void populateListView_trial() {

blockedApps = loadArrayBlockedApps();

if(isTimerRunning()) {
blockedApps = loadArrayBlockedApps();
temp_blockedList = new ArrayAdapter<String>(
MainActivityCircularSeekbar.this,
android.R.layout.simple_list_item_1, blockedApps);
} else {

holder = new ArrayList<Datamodel>();


for(Map.Entry<String, String> entry : list_installedApps.entrySet()) {

Datamodel _appdata = new Datamodel();
_appdata.setAppname(entry.getKey());
_appdata.setSelected(true);

try {
_appdata.setAppIcon(getIconFromPackageName(entry.getValue(), this));
} catch (Exception e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
}

holder.add(_appdata);
}

_adapter = new MyAdapter(getApplicationContext(), holder);
try {

lv_apps.setAdapter(_adapter);

} catch (Exception e) {
Log.v(TAG_debug, " caught exception in line 2097, STACKTRACE =");
}

}

}



@SuppressWarnings("null")
private Map<String, String> getInstalledAppsList() {
List<String> l_installedApps = new ArrayList<String>();

//List<Map<String, String>> map_installedApps = new ArrayList<Map<String, String>>();

Map<String, String> map_installedApps = new HashMap<String, String>();

List<PackageInfo> allPackagesList = getPackageManager().getInstalledPackages(PackageManager.GET_META_DATA);

PackageManager pm = getPackageManager();

for(int i = 0 ; i < allPackagesList.size() ; i++) {
PackageInfo p = allPackagesList.get(i);
String appname = p.applicationInfo.loadLabel(pm).toString();
String packageName = p.packageName;

map_installedApps.put(appname, packageName);

//l_installedApps.add(appname);
}

return map_installedApps;
}

违规行:05-26 22:35:44.304: E/AndroidRuntime(4168): java.lang.IllegalStateException: 观察者 android.widget.AbsListView$AdapterDataSetObserver@529347d0 未注册。

我该如何解决这个问题???我需要更新 ListView 中的各种元素。

05-26 22:35:40.408: E/OpenGLRenderer(4168): Getting MAX_TEXTURE_SIZE from GradienCache
05-26 22:35:40.412: E/OpenGLRenderer(4168): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
05-26 22:35:44.304: E/AndroidRuntime(4168): FATAL EXCEPTION: main
05-26 22:35:44.304: E/AndroidRuntime(4168): java.lang.IllegalStateException: Observer android.widget.AbsListView$AdapterDataSetObserver@529347d0 was not registered.
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.database.Observable.unregisterObserver(Observable.java:69)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.widget.BaseAdapter.unregisterDataSetObserver(BaseAdapter.java:42)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.widget.AbsListView.onDetachedFromWindow(AbsListView.java:2638)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.view.View.dispatchDetachedFromWindow(View.java:12160)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2570)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2568)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:3960)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.view.ViewGroup.removeAllViews(ViewGroup.java:3904)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.widget.TabHost.clearAllTabs(TabHost.java:251)
05-26 22:35:44.304: E/AndroidRuntime(4168): at com.mavdev.focusoutfacebook.MainActivityCircularSeekbar$LoadALLAppsList.onPostExecute(MainActivityCircularSeekbar.java:2480)
05-26 22:35:44.304: E/AndroidRuntime(4168): at com.mavdev.focusoutfacebook.MainActivityCircularSeekbar$LoadALLAppsList.onPostExecute(MainActivityCircularSeekbar.java:1)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.os.AsyncTask.finish(AsyncTask.java:631)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.os.Looper.loop(Looper.java:137)
05-26 22:35:44.304: E/AndroidRuntime(4168): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-26 22:35:44.304: E/AndroidRuntime(4168): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 22:35:44.304: E/AndroidRuntime(4168): at java.lang.reflect.Method.invoke(Method.java:525)
05-26 22:35:44.304: E/AndroidRuntime(4168): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-26 22:35:44.304: E/AndroidRuntime(4168): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-26 22:35:44.304: E/AndroidRuntime(4168): at dalvik.system.NativeStart.main(Native Method)
05-26 22:36:56.648: E/OpenGLRenderer(4237): Getting MAX_TEXTURE_SIZE from GradienCache
05-26 22:36:56.656: E/OpenGLRenderer(4237): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
05-26 22:37:00.256: E/AndroidRuntime(4237): FATAL EXCEPTION: main
05-26 22:37:00.256: E/AndroidRuntime(4237): java.lang.IllegalStateException: Observer android.widget.AbsListView$AdapterDataSetObserver@529379f0 was not registered.
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.database.Observable.unregisterObserver(Observable.java:69)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.widget.BaseAdapter.unregisterDataSetObserver(BaseAdapter.java:42)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.widget.AbsListView.onDetachedFromWindow(AbsListView.java:2638)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.view.View.dispatchDetachedFromWindow(View.java:12160)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2570)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2568)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:3960)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.view.ViewGroup.removeAllViews(ViewGroup.java:3904)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.widget.TabHost.clearAllTabs(TabHost.java:251)
05-26 22:37:00.256: E/AndroidRuntime(4237): at com.mavdev.focusoutfacebook.MainActivityCircularSeekbar$LoadALLAppsList.onPostExecute(MainActivityCircularSeekbar.java:2480)
05-26 22:37:00.256: E/AndroidRuntime(4237): at com.mavdev.focusoutfacebook.MainActivityCircularSeekbar$LoadALLAppsList.onPostExecute(MainActivityCircularSeekbar.java:1)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.os.AsyncTask.finish(AsyncTask.java:631)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.os.Looper.loop(Looper.java:137)
05-26 22:37:00.256: E/AndroidRuntime(4237): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-26 22:37:00.256: E/AndroidRuntime(4237): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 22:37:00.256: E/AndroidRuntime(4237): at java.lang.reflect.Method.invoke(Method.java:525)
05-26 22:37:00.256: E/AndroidRuntime(4237): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-26 22:37:00.256: E/AndroidRuntime(4237): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-26 22:37:00.256: E/AndroidRuntime(4237): at dalvik.system.NativeStart.main(Native Method)

最佳答案

正确填充 ListView 的关键是使用 arrayAdapter:只需更新数组,清除 ListView ,添加新项目,并通知 ListView ,它的数据已更改:

private ArrayAdapter<String> itemsAdapter = 
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, loadArrayBlockedApps());

初始化 View 时,确保 arrayAdapter 链接到 listView:

ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(itemsAdapter);

现在在您想要实际调整数组的方法中执行如下操作:

itemsAdapter.clear();
itemsAdapter.addAll(loadArrayBlockedApps());
itemsAdapter.notifyDatasetChanged();

关于android - 为什么执行 listview.setAdapter(_adapter) 两次会使应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23880758/

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