Почему listview.setAdapter (_adapter) дважды приводит к сбою приложения?

Ключевой вопрос, который у меня есть, – это 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) 

Solutions Collecting From Web of "Почему listview.setAdapter (_adapter) дважды приводит к сбою приложения?"

Ключ к правильному заполнению списка – это использовать arrayAdapter: просто обновите массив, очистите список, добавьте новые элементы и уведомите listview, его данные изменились:

 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();