运行异常:获取不到UI界面组件对象

java.lang.RuntimeException: Unable to start activity ComponentInfo{wz.com.lessonokhttp/wz.com.lessonokhttp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.TextView.setText(java.lang.CharSequence)’ on a null object reference

解决方案

1.查看是否添加相应布局

setContentView(R.layout.activity_main);

2.查看是否实例化组件(通过id值获取组件)

txt=(TextView) this.findViewById(R.id.show_data);

3.查看组件的id值是否正确(是否有冲突)

  <TextView
                android:text="TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/show_data" />

不积跬步无以至千里。

读万卷书,行万里路。

水滴石穿

加油!

猜你喜欢

转载自blog.csdn.net/fruit_fruit/article/details/78728939
今日推荐