2010-03-02 41 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) { 
} 

tôi đã nhận được ngoại lệ nullpointer.android textview settext show nullpointer exception

giải pháp cho việc này là gì. vui lòng trả lời tôi

Trả lời

6

Vâng, có vẻ như là findViewById trả lại null. Đây có thể là trường hợp, vì this không tham chiếu đến Hoạt động hoặc bạn không chỉ định Chế độ xem cho Hoạt động trong onCreate (bạn phải gọi setContentView trong onCreate).

+0

cảm ơn, bạn có mẫu – saravanan

+1

cảm ơn jleedev tôi đã nhận được đầu ra không, thực sự tôi nhầm lẫn vị trí phương thức setcontentview. bây giờ tôi đã bị xóa – saravanan

Các vấn đề liên quan