我正在尝试为 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/
我已经知道如何制作按钮来切换布局,但我无法在不崩溃的情况下显示整数。:(