Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Observer ... was not registered" error

Tags:

android

I'm working on an Android application, and when I attempt to display large amounts of data in a listview, I receive the following error:

01-24 18:06:32.214: 
INFO/System.out(9244): 
  EX:Observer android.widget.AdapterView$AdapterDataSetObserver@43c9b200 
  was not registered.
like image 327
user533787 Avatar asked Jan 24 '11 12:01

user533787


3 Answers

i got this error too.
in my case this exception (Observer * was not registered.) was thrown because i called

setAdapter(Adapter)

multiple times.

for example:

MyAdapter (extends BaseAdapter) vla;
ListView lv;

for (int i = 0; i < 2; i++) {
  vla.clear();
  vla.add(tv);
  lv.setAdapter(vla);
}

The fix here is to place
v.setAdapter(vla);
outside the loop, thereby avoiding multiple calls.

after fix no more exception throws.
best regards, flood. (fldr atincircle mail dot ru)

like image 193
flood Avatar answered Nov 07 '22 01:11

flood


I faced the same problem and it turned out to be a threading issue. Multiple threads were calling setAdapter which does not seem to be threadsafe. After making sure that all setAdapter calls are done on the same thread, the problem was solved.

What worked nicely for me, was to force my code to change it only on the UI thread:

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rc_view);
                recyclerView.setAdapter(new MyAdapter(items));
            }
        });
like image 3
Holger Brandl Avatar answered Nov 07 '22 03:11

Holger Brandl


I've ran into this problem multiple times when dealing with listviews. If you're using a custom adapter with an edit text involved, you'll want to do a clearFocus() before you refresh the adapter. Observer not registered is caused by the listview deleting a child that has focus on the activity

How I do it: this is the view holder for my custom adapter

class viewHolder {
    public viewHolder() {
        views = new ArrayList<viewInfo>();
    }

    public int position;
    public ArrayList<viewInfo> views;
    public View view;
}

After which i invoke an unfocusAll() which consists of:

public void unFocusAll() {
    viewHolder holder = (viewHolder) v.getTag();
        if (holder != null) {
            holder.view.clearFocus();
        }
}

So before you invoke your listview you have to do

CustomAdapter aa = new CustomAdapter();
if (aa != null) {
  aa.unFocusAll();
}
like image 2
MrPancake Avatar answered Nov 07 '22 02:11

MrPancake