我有一个非常简单的 2 活动应用程序、标题屏幕和主游戏。我正在努力做到这一点,以便当用户按下锁定按钮时,当他们解锁手机时,该应用程序将重新出现在标题屏幕中。我遇到的问题是,超过一半的时间,当我再次解锁手机时,标题屏幕活动出现异常。这是我到目前为止所得到的,我知道这可能不是最好的方法,但我有点挣扎。这是我在主要游戏活动中所做的
// On pause, return to main title page
@Override
public void onPause() {
super.onPause();
// Create intent to go back to the title menu
Intent intent = new Intent(this, ActivityTitle.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
这就是导致标题栏中错误的原因
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title_screen);
// Make sure that the volume buttons change the media volume in the app
setVolumeControlStream(AudioManager.STREAM_MUSIC);
radioGroupSound = (RadioGroup) findViewById(R.id.radioGroupSound);
radioGroupText = (RadioGroup) findViewById(R.id.radioGroupText);
// Make the start text blink
TextView myText = (TextView) findViewById(R.id.textViewStart );
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(500); //You can manage the time of the blink with this parameter
anim.setStartOffset(500);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
myText.startAnimation(anim);
}
这是导致异常的最后一行 - myText.startAnimation(anim)。
这是错误日志
07-19 19:49:52.729 26036-26036/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chrisbjohnson.colorbubbles/com.chrisbjohnson.colorbubbles.ActivityTitle}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:945)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3719)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.chrisbjohnson.colorbubbles.ActivityTitle.onCreate(ActivityTitle.java:44)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
任何想法将不胜感激。要么是一种不同的处理方式,要么是一种解决我正在做的事情的方法。
谢谢。