Why is using TextView to update int value and display it giving me a crash?-Collection of common programming errors

Im trying to develop a text adventure for android. Already found a subtle way to change ‘pages’ without creating new Activity.

Now I was trying to include permanent int values on each of my layouts that would get updated immediately when changed.

I.e. gold/health values. Depending on the choice of player ingame, it would: – change the layout to the corresponding page – change int value of Gold +10, health -1

I developed this code in the mainactivity class:

package com.example.textadventure;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    private int int1 = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void changelayout1(View view){
        setContentView(R.layout.activity_main2);
        int1 += 1;
    }
    public void changelayout2(View view){
        setContentView(R.layout.activity_main);
        int1 += 1;
    }
    public void changelayout3(View view){
        setContentView(R.layout.activity_main3);
        int1 += 1;
    }



    public void onResume() {
        super.onResume();
        TextView textView = (TextView) this.findViewById(R.id.mytextview1);
        textView.setText(String.valueOf(int1));
    }



}

with the following result:

My LogCat

04-19 16:09:57.446: E/Trace(3204): error opening trace file: No such file or directory (2)
04-19 16:09:59.016: D/AndroidRuntime(3204): Shutting down VM
04-19 16:09:59.016: W/dalvikvm(3204): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-19 16:09:59.036: E/AndroidRuntime(3204): FATAL EXCEPTION: main
04-19 16:09:59.036: E/AndroidRuntime(3204): java.lang.RuntimeException: Unable to resume activity {com.example.textadventure/com.example.textadventure.MainActivity}: java.lang.NullPointerException
04-19 16:09:59.036: E/AndroidRuntime(3204):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at android.os.Looper.loop(Looper.java:137)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at java.lang.reflect.Method.invoke(Method.java:511)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at dalvik.system.NativeStart.main(Native Method)
04-19 16:09:59.036: E/AndroidRuntime(3204): Caused by: java.lang.NullPointerException
04-19 16:09:59.036: E/AndroidRuntime(3204):     at com.example.textadventure.MainActivity.onResume(MainActivity.java:28)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at android.app.Activity.performResume(Activity.java:5182)
04-19 16:09:59.036: E/AndroidRuntime(3204):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
04-19 16:09:59.036: E/AndroidRuntime(3204):     ... 12 more
04-19 16:10:02.386: I/Process(3204): Sending signal. PID: 3204 SIG: 9
04-19 16:15:42.576: D/AndroidRuntime(3292): Shutting down VM
04-19 16:15:42.586: W/dalvikvm(3292): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-19 16:15:42.596: E/AndroidRuntime(3292): FATAL EXCEPTION: main
04-19 16:15:42.596: E/AndroidRuntime(3292): java.lang.RuntimeException: Unable to resume activity {com.example.textadventure/com.example.textadventure.MainActivity}: java.lang.NullPointerException
04-19 16:15:42.596: E/AndroidRuntime(3292):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at android.os.Looper.loop(Looper.java:137)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at java.lang.reflect.Method.invoke(Method.java:511)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at dalvik.system.NativeStart.main(Native Method)
04-19 16:15:42.596: E/AndroidRuntime(3292): Caused by: java.lang.NullPointerException
04-19 16:15:42.596: E/AndroidRuntime(3292):     at com.example.textadventure.MainActivity.onResume(MainActivity.java:31)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at android.app.Activity.performResume(Activity.java:5182)
04-19 16:15:42.596: E/AndroidRuntime(3292):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
04-19 16:15:42.596: E/AndroidRuntime(3292):     ... 12 more
04-19 16:18:00.358: E/Trace(3350): error opening trace file: No such file or directory (2)
04-19 16:18:01.796: D/AndroidRuntime(3350): Shutting down VM
04-19 16:18:01.810: W/dalvikvm(3350): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-19 16:18:01.866: E/AndroidRuntime(3350): FATAL EXCEPTION: main
04-19 16:18:01.866: E/AndroidRuntime(3350): java.lang.RuntimeException: Unable to resume activity {com.example.textadventure/com.example.textadventure.MainActivity}: java.lang.NullPointerException
04-19 16:18:01.866: E/AndroidRuntime(3350):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at android.os.Looper.loop(Looper.java:137)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at java.lang.reflect.Method.invoke(Method.java:511)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at dalvik.system.NativeStart.main(Native Method)
04-19 16:18:01.866: E/AndroidRuntime(3350): Caused by: java.lang.NullPointerException
04-19 16:18:01.866: E/AndroidRuntime(3350):     at com.example.textadventure.MainActivity.onResume(MainActivity.java:31)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at android.app.Activity.performResume(Activity.java:5182)
04-19 16:18:01.866: E/AndroidRuntime(3350):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
04-19 16:18:01.866: E/AndroidRuntime(3350):     ... 12 more
04-19 16:22:06.477: E/Trace(3412): error opening trace file: No such file or directory (2)
04-19 16:22:08.086: D/AndroidRuntime(3412): Shutting down VM
04-19 16:22:08.116: W/dalvikvm(3412): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-19 16:22:08.207: E/AndroidRuntime(3412): FATAL EXCEPTION: main
04-19 16:22:08.207: E/AndroidRuntime(3412): java.lang.RuntimeException: Unable to resume activity {com.example.textadventure/com.example.textadventure.MainActivity}: java.lang.NullPointerException
04-19 16:22:08.207: E/AndroidRuntime(3412):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at android.os.Looper.loop(Looper.java:137)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at java.lang.reflect.Method.invoke(Method.java:511)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at dalvik.system.NativeStart.main(Native Method)
04-19 16:22:08.207: E/AndroidRuntime(3412): Caused by: java.lang.NullPointerException
04-19 16:22:08.207: E/AndroidRuntime(3412):     at com.example.textadventure.MainActivity.onResume(MainActivity.java:35)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at android.app.Activity.performResume(Activity.java:5182)
04-19 16:22:08.207: E/AndroidRuntime(3412):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
04-19 16:22:08.207: E/AndroidRuntime(3412):     ... 12 more
04-19 16:22:10.786: I/Process(3412): Sending signal. PID: 3412 SIG: 9
04-19 16:25:22.020: D/AndroidRuntime(3477): Shutting down VM
04-19 16:25:22.020: W/dalvikvm(3477): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-19 16:25:22.026: E/AndroidRuntime(3477): FATAL EXCEPTION: main
04-19 16:25:22.026: E/AndroidRuntime(3477): java.lang.RuntimeException: Unable to resume activity {com.example.textadventure/com.example.textadventure.MainActivity}: java.lang.NullPointerException
04-19 16:25:22.026: E/AndroidRuntime(3477):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at android.os.Looper.loop(Looper.java:137)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at java.lang.reflect.Method.invoke(Method.java:511)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at dalvik.system.NativeStart.main(Native Method)
04-19 16:25:22.026: E/AndroidRuntime(3477): Caused by: java.lang.NullPointerException
04-19 16:25:22.026: E/AndroidRuntime(3477):     at com.example.textadventure.MainActivity.onResume(MainActivity.java:36)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at android.app.Activity.performResume(Activity.java:5182)
04-19 16:25:22.026: E/AndroidRuntime(3477):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
04-19 16:25:22.026: E/AndroidRuntime(3477):     ... 12 more

As i said i want to have something like this:

http://postimg.org/image/dy7g2ts6v/

I already know how to make buttons to switch layouts, but I can’t display the integers without crash. 🙁

  1. Do it like this:

    private Integer int1 = 1;
    

    and below

    TextView textView = (TextView) findViewById(R.id.mytextview1);
    textView.setText(int1.toString());
    

    Notice you don’t need this.findViewById , just use findViewById directly.

  2. TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv= (TextView) findViewById(R.id.tv);
        //textview with id should be defined in activity_main.xml 
        //set text to textview.
    }
    

    Make sure you have a text view in activity_main. since you have set the layout with activity_main you need to make sure you have textview in activity_main

    http://developer.android.com/training/basics/activity-lifecycle/starting.html

    You must implement the onCreate() method to perform basic application startup logic that should happen only once for the entire life of the activity. You can initialize textiew in oncreate.

    Suppose activity pauses and resumes onReusme() is called, then evry time you wwould be finding the id of textview. So initialize textiew in oncreate.

    It is better to start a new activity or use fragments instead of setting different layout’s to the same screen.

    activity_main