android textview settext显示nullpointer异常

sar*_*nan 5 android textview

<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) {
}
Run Code Online (Sandbox Code Playgroud)

我得到了nullpointer异常.

什么是解决方案.请回复我

Tho*_*mar 6

好吧,好像findViewById回来了null.这大概就是这样,因为this不请参考活动,或者你没有指定的视图中的活动onCreate(你必须调用setContentViewonCreate).