开发者

android textview settext show nullpointer exception

开发者 https://www.devze.com 2022-12-21 12:03 出处:网络
<TextView android:id=\"@+id/first_name\" android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\"
<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"
a开发者_开发百科ndroid: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) {
}

i got the nullpointer exception.

what is solution for this. please reply me


Well, seems that findViewById returns null. This is probably the case, because this does not refer to the Activity or you're not assigning the View to the Activity in onCreate (you have to call setContentView in onCreate).

0

精彩评论

暂无评论...
验证码 换一张
取 消