简体   繁体   中英

setText() in onCreate causes crash (android)

I'm trying to make a TextView change its text on startup, but as soon as the code has compiled the app crashes.

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"); 
}

I've tried all the solutions I've found by googling, but none of them have worked for me. This should be easy to solve I think, but maybe I've stared at it for to long. This is just a test, I'm trying to figure out how to set texts programmatically, hence why I havn't just set the text in the XML-file.

EDIT:

In my "Fragment_main.xml" I've got a few more views, but I'll link the layout and the textview in question:

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

Don't know much about the LogCat, but I filtered it for errors:

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

change this line

setContentView(R.layout.activity_main);

to

setContentView(R.layout.fragment_main);//give the proper layout name which has textview named textview_news

because textview_news this TextView isn't present in activity_main layout thats why you get NPE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM