为什么使用TextView更新int值并显示它给我一个崩溃?

时间:2013-04-21 11:10:13

标签: java android android-layout dynamic textview

我正在尝试为Android开发文本冒险。已经找到了一种在不创建新活动的情况下更改“页面”的微妙方法。

现在我试图在我的每个布局上包含永久int值,这些布局会在更改时立即更新。

即。黄金/健康价值。根据玩家游戏的选择,它会: - 将布局更改为相应的页面 - 更改金币+10的内部值,健康-1

我在mainactivity类中开发了这段代码:

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



}

,结果如下:

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

正如我所说,我希望有这样的东西:

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

我已经知道如何设置按钮来切换布局,但是我无法在没有崩溃的情况下显示整数。 :(

3 个答案:

答案 0 :(得分:0)

似乎并非所有布局都具有视图mytextview1。确保每个布局XML中都存在这样的视图。

考虑使用两个片段,一个用于显示永久信息,另一个用于根据需要在布局之间切换。

片段指南:

http://developer.android.com/training/basics/fragments/index.html

http://developer.android.com/guide/components/fragments.html

答案 1 :(得分:0)

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

确保您在activity_main中有文本视图。由于您已使用activity_main设置布局,因此您需要确保在activity_main

中包含textview

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

您必须实现onCreate()方法来执行基本的应用程序启动逻辑,该逻辑在活动的整个生命周期中只应发生一次。您可以在oncreate中初始化textiew。

假设活动暂停并恢复onReusme()被调用,那么你将会找到textview的id。所以在oncreate中初始化textiew。

最好开始一项新活动或使用片段,而不是将不同的布局设置到同一屏幕。

activity_main

<LinearLayout 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:orientation="vertical"
tools:context=".DisplayMessageActivity" >

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

答案 2 :(得分:0)

这样做:

private Integer int1 = 1;

及以下

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

请注意,您不需要this.findViewById,只需直接使用findViewById。