2
public class MyActivity extends ActionBarActivity {

    @Override
    public void onContentChanged() {
        TextView tv = (TextView) findViewById(R.id.asdasd);
        // tv is NULL on Android 2.3
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

On Android device v.4.2, findViewById() returns the correct view, whereas on 2.3 it basically returns null. What's the difference?

UPDATE: From what I've tried: It seems to me that somehow when extending from ActionBarActivity (not directly from Activity of 2.3) The changes are not yet in place when contentChanged is being called, but when extending from Activity of 2.3 the method call is on time. For the time being an ugly hack which seems to be working is: I have a superclass for MyActivity which extends from ActionBarActivity, there I override all setContentView methods like this:

@Override
    public void setContentView(View view) {
        super.setContentView(view);
        afterSetContentView = true;
        onContentChanged();
    }

and inside my onContentChanged, I check afterSetContentView and then use findViewById.

UPDATE 2: Seemingly, it's a bug in support libraries, I've reported it and it got assigned, so they're fixing it now: https://code.google.com/p/android/issues/detail?id=59445&q=onContentChanged&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

1
  • 1
    Glad to see you got it working. You should add your "UPDATE 2" edit as an answer and accept it so people will know that this issue has been fixed. :) Aug 30, 2013 at 18:03

1 Answer 1

1

As it turned out, it was a bug in Support Library. See details here: https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=onContentChanged&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=59445

1
  • UPDATE: Today they did a release, check it out.
    – inteloid
    Nov 1, 2013 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.