我正在尝试让TextView在启动时更改其文本,但是一旦代码编译应用程序崩溃。
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.textview_news);
ok = (Button) findViewById(R.id.button_full);
txt.setText("text");
}
我已经尝试了通过谷歌搜索找到的所有解决方案,但它们都没有为我工作。我认为这应该很容易解决,但也许我已经盯着它看了很长时间。这只是一个测试,我试图弄清楚如何以编程方式设置文本,因此我不仅仅是在XML文件中设置文本。
编辑:
在我的“Fragment_main.xml”中,我还有一些视图,但我会链接布局和有问题的textview:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="rasmus.projectzero.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textview_news"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:onClick="SwitchToNews"
android:clickable="true" />
</TableLayout>
对LogCat了解不多,但我过滤了错误:
E/AndroidRuntime(833): FATAL EXCEPTION: main
E/AndroidRuntime(833): Process: rasmus.projectzero, PID: 833
E/AndroidRuntime(833): java.lang.RuntimeException: Unable to start activity ComponentInfo{rasmus.projectzero/rasmus.projectzero.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(833): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
E/AndroidRuntime(833): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
E/AndroidRuntime(833): at android.app.ActivityThread.access$800(ActivityThread.java:135)
E/AndroidRuntime(833): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
E/AndroidRuntime(833): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(833): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(833): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(833): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(833): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(833): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(833): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(833): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(833): Caused by: java.lang.NullPointerException
E/AndroidRuntime(833): at rasmus.projectzero.MainActivity.onCreate(MainActivity.java:28)
E/AndroidRuntime(833): at android.app.Activity.performCreate(Activity.java:5231)
E/AndroidRuntime(833): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(833): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
E/AndroidRuntime(833): ... 11 more
答案 0 :(得分:0)
更改此行
setContentView(R.layout.activity_main);
到
setContentView(R.layout.fragment_main);//give the proper layout name which has textview named textview_news
因为textview_news
activity_main
这个TextView没有出现在NPE
布局中,这就是为什么你会得到{{1}}。