Why doing listview.setAdapter(_adapter) twice crashes app?
Asked Answered
J

1

8

The key issue I have is doing lv_apps.setAdapter(_adapter) TWICE is what crashes my app. (Basically, when i call populateListView_trial() the 2nd time, app crashes -- and the offending line is lv_apps.setAdapter(_adapter)

Stacktrace is below the functions

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;   
}

Offending line: 05-26 22:35:44.304: E/AndroidRuntime(4168): java.lang.IllegalStateException: Observer android.widget.AbsListView$AdapterDataSetObserver@529347d0 was not registered.

How can i get around this??? I need to update various elements in the 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)
Jura answered 27/5, 2014 at 3:43 Comment(6)
pls see this link #14424530 grokkingandroid.com/use-contentobserver-to-listen-to-changesFlieger
Why are you calling popullate listview twice?Carminacarminative
@Carminacarminative - This function gets called on a button click. So if user clicks on the button twice, it gets called twice. I need to run the code every time because i plan to show only certain apps in the list based on some setting changes in the app.Jura
The stack trace doesn't seem related with your code. There's no AsyncTask in the code. Please post the exact code. Or the correct stacktrace.Leasia
Don't call setAdapter() twice, update the data in the adapter and call notifyDataSetChanged()Eliezer
#8508794Epiboly
E
0

The key to populating the listview correctly is using an arrayAdapter: just update the array, clear the listview, add the new items, and notify the listview, it's data has changed:

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

When initializing the view, make sure the arrayAdapter is linked to the listView:

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

Now in the method where you want to actually adapt the array do something like this:

itemsAdapter.clear();
itemsAdapter.addAll(loadArrayBlockedApps());
itemsAdapter.notifyDatasetChanged();
Enneahedron answered 9/5, 2015 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.