0

我正在尝试为 android 开发文本冒险。已经找到了一种无需创建新活动即可更改“页面”的微妙方法。

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

即黄金/健康值。根据玩家在游戏中的选择,它将: - 将布局更改为相应的页面 - 更改金币 +10 的 int 值,健康 -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));
    }



}

结果如下:

我的日志猫

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/

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

4

3 回答 3

0

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

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

碎片指南:

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

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

于 2013-04-21T11:14:06.380 回答
0

像这样做:

private Integer int1 = 1;

及以下

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

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

于 2013-04-21T12:04:42.080 回答
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。

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

活动主

<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>
于 2013-04-21T11:28:24.797 回答