2010-03-02 120 views
5
<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显示空指针异常

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

回答

6

好吧,好像是findViewById返回null。这大概就是这样,因为this并不是指活动或者你并没有使用在onCreate观对活动(你必须调用onCreatesetContentView)。

+0

谢谢,你有样品 – saravanan 2010-03-02 07:54:14

+1

谢谢jleedev我得到了输出,其实我混淆了setcontentview方法的位置。现在我被清除 – saravanan 2010-03-02 08:19:59