<TextView
android:id="@+id/first_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:textColor="@color/light_grey"
android:text="FirstName"
></TextView>
TextView first_text_view;
first_text_view = (TextView)this.findViewById(R.id.first_name);
try {
first_text_view.setText("Android");
}catch(Exception e) {
}
我得到了空指针异常。
解决这个问题的方法是什么。请回复我
最佳答案
好吧,findViewById
似乎返回了 null
。这可能是这种情况,因为 this
没有引用 Activity,或者您没有在 onCreate
中将 View 分配给 Activity(您必须调用 setContentView
在 onCreate
中)。
关于android textview settext 显示空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2361509/