简体   繁体   English

Android onContentChanged on 2.3 vs 4.2

[英]Android onContentChanged on 2.3 vs 4.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 . 在Android设备v.4.2上, findViewById()返回正确的视图,而在2.3上,它基本上返回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. 更新:从我尝试过:从我看来,从ActionBarActivity扩展(不是直接从2.3的活动直接扩展)时,调用contentChanged时更改尚未到位,但是从2.3的Activity扩展时,方法调用准时。 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: 就目前看来,似乎是在起作用的一个丑陋的hack是:我有一个MyActivity的超类,它是从ActionBarActivity扩展的,在那里我覆盖了所有这样的setContentView方法:

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

and inside my onContentChanged, I check afterSetContentView and then use findViewById. 在我的onContentChanged内部,我检查afterSetContentView,然后使用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 更新2:看来,这是支持库中的一个错误,我已经报告了它并将它分配给它,所以他们现在正在解决它: https : //code.google.com/p/android/issues/detail?id= 59445&q = onContentChanged&colspec = ID%20Type%20Status%20Owner%20Summary%20Stars

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM