android textview settext显示空指针异常
问题描述:
<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) {
}
我得到了空指针异常。android textview settext显示空指针异常
什么是该解决方案。请回复我
答
好吧,好像是findViewById
返回null
。这大概就是这样,因为this
并不是指活动或者你并没有使用在onCreate
观对活动(你必须调用onCreate
setContentView
)。
谢谢,你有样品 – saravanan 2010-03-02 07:54:14
谢谢jleedev我得到了输出,其实我混淆了setcontentview方法的位置。现在我被清除 – saravanan 2010-03-02 08:19:59