我正在尝试开发一个应用程序。它有一些活动,这些是,
SplashActivity
,
MainMenuActivity
,和
GameActivity
。
现在,所有三个活动都扩展了BaseActivity
,这也扩展了libgdx的AndroidApplication
。
在SplashActivity
和MainMenuActivity
中,我使用onCreate编写了纯机器人专用代码,setContentView使用了布局等等。我没有在这些活动上编写任何libgdx代码。
活动过渡是这样的, SplashActivity(它完成) - > MainMenuActivity(未完成) - > GameActivity
On,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
答案 0 :(得分:2)
这部分堆栈跟踪:
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)
表示您的SplashActivity
扩展了Libgdx AndroidApplication
。但你写道:
在SplashActivity,MainMenuActivity中,我编写了纯机器人 特定代码使用onCreate,setcontentView布局等我有 不要在那些活动上写任何libgdx代码。
但是,如果您正在扩展AndroidApplication
,那么您将获得一堆在暂停和恢复时运行的Libgdx代码。但是你还没有初始化Libgdx(因为这似乎是你GameActivity
创作的副作用。
保持纯粹的Android活动纯净,不要扩展Libgdx类。