سؤال

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

هل كانت مفيدة؟
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top