以下代码不起作用,并引发由NullPointerException引起的RuntimeException
public class ListFilteredActivity extends Activity {
LinearLayout typeSelector = new LinearLayout(this) ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
this.setContentView(sv);
//this.typeSelector = new LinearLayout(this);
this.typeSelector.setOrientation(LinearLayout.VERTICAL);
sv.addView(this.typeSelector);
}
当我在onCreate()中移动this.typeSelection的初始化时,它的效果很好.
@Override
public void onCreate(Bundle savedInstanceState) {
...
this.typeSelector = new LinearLayout(this);
...
}
为什么会出现空指针错误?第一段代码中的内联声明在调用构造函数后立即发生,然后onCreate()可以访问该对象,不是吗?
最佳答案
原文链接:https://www.f2er.com/android/531554.html