我遇到的关键问题是 lv_apps.setAdapter(_adapter) TWICE 会导致我的应用崩溃。 (基本上,当我第二次调用 populateListView_trial()时,应用程序崩溃 - 并且违规行是 lv_apps.setAdapter(_adapter)
Stacktrace位于函数
之下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:Observer android.widget.AbsListView$AdapterDataSetObserver@529347d0未注册。
我怎么能绕过这个?我需要更新列表视图中的各种元素。
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)
答案 0 :(得分:0)
正确填充listview的关键是使用arrayAdapter: 只需更新数组,清除列表视图,添加新项目并通知列表视图,它的数据已更改:
private ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, loadArrayBlockedApps());
初始化视图时,请确保arrayAdapter链接到listView:
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(itemsAdapter);
现在,在您想要实际调整数组的方法中,执行以下操作:
itemsAdapter.clear();
itemsAdapter.addAll(loadArrayBlockedApps());
itemsAdapter.notifyDatasetChanged();