我嘗試開發一個應用程序。它有一些活動,它們是: SplashActivity
, MainMenuActivity
和 GameActivity
。無法恢復在libGDX中的活動
現在,所有三項活動延伸到BaseActivity
,這也擴展了libgdx的AndroidApplication
。
在SplashActivity
和MainMenuActivity
,我寫了純粹的android特定的代碼,使用onCreate,setContentView佈局等。我沒有在這些活動上寫任何libgdx代碼。
活動轉變是這樣的, SplashActivity(它得到完成) - > MainMenuActivity(不拿完) - > GameActivity
上,GameActivity,我的setContentView mygame佈局,並寫這樣的代碼:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mygame);
gbbrdlyot1 = (RelativeLayout) findViewById(R.id.gbbrdlyot1);
gameBoard = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newBlockWidth, newBlockWidth);
gameBoard.setLayoutParams(layoutParams);
View gameView = initializeForView(new GameView(), true);
gameBoard.addView(gameView);
gbbrdlyot1.addView(gameBoard);
}
但我對SplashActivity越來越RuntimeException的,我不知道爲什麼,
05-27 12:56:22.609: java.lang.RuntimeException: Unable to resume activity com.game.MyGame/com.game.MyGame.SplashActivity}: java.lang.NullPointerException
05-27 12:56:22.609: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
05-27 12:56:22.609: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
05-27 12:56:22.609: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
05-27 12:56:22.609: at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-27 12:56:22.609: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-27 12:56:22.609: at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 12:56:22.609: at android.os.Looper.loop(Looper.java:130)
05-27 12:56:22.609: at android.app.ActivityThread.main(ActivityThread.java:3687)
05-27 12:56:22.609: at java.lang.reflect.Method.invokeNative(Native Method)
05-27 12:56:22.609: at java.lang.reflect.Method.invoke(Method.java:507)
05-27 12:56:22.609: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-27 12:56:22.609: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-27 12:56:22.609: at dalvik.system.NativeStart.main(Native Method)
05-27 12:56:22.609: Caused by: java.lang.NullPointerException
05-27 12:56:22.609: at com.badlogic.gdx.backends.android.AndroidApplication.onResume(AndroidApplication.java:249)
05-27 12:56:22.609: at com.game.MyGame.SplashActivity.onResume(SplashActivity.java:43)
05-27 12:56:22.609: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
05-27 12:56:22.609: at android.app.Activity.performResume(Activity.java:3832)
05-27 12:56:22.609: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
05-27 12:56:22.609: ... 12 more
正如PT說,你的SplashActivity沒有初始化libgdx,你可以看到AndroidApplication的第249行是((AndroidInput)getInput())。registerSensorListeners();當你調用AndroidApplication#initialize(..)在SplashActivity#onCreate()中調用,這就是拋出NullPointerException的原因。 –