Android的TextView中的setText显示空指针异常 [英] android textview settext show nullpointer exception

查看:358
本文介绍了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) {
}

我得到了空指针异常。

i got the nullpointer exception.

什么是这个解决方案。请回复我

what is solution for this. please reply me

推荐答案

唉,看来 findViewById 返回 。这可能是这样,因为不请参考活动或者你并没有使用视图到活动中的onCreate (你必须调用的setContentView 的onCreate )。

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).

这篇关于Android的TextView中的setText显示空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆