我正在寫一款遊戲,並且我需要在遊戲結束時切換活動。我的問題是我有一個擴展到Activity的gameLogic Java類,它不是一個主要的活動類。Android中無法啓動新活動
所以不得不在GameLogic類一個GAMEOVER方法,它返回布爾值。如果它返回false,它必須切換到第二個Activity,但它不會。它返回null.pointer.exception
。
下面是代碼:
public void gameOver(GameData data){
if(!intersect(data)){
Intent intent = new Intent(GameLogic.this , SecondActivity.class);
startActivity(intent);
}
}
和我打電話GAMEDATA類這種方法在OnDraw的方法,因爲OnDraw的方法總是有能力控制它是否GAMEOVER與否
protected void onDraw(Canvas canvas){
.........
....
......
.....
............
logic.gameOver(this);
}
錯誤:
08-20 14:13:58.293 29830-29830/com.example.msimsi.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.msimsi.myapplication, PID: 29830
java.lang.NullPointerException: Attempt to invoke virtual method'java.lang.String android.content.Context.getPackageName()' on a null object reference at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132) at android.content.ComponentName.(ComponentName.java:77) at android.content.Intent.(Intent.java:4160) at com.example.msimsi.myapplication.GameLogic.gameOver(GameLogic.java:230) at com.example.msimsi.myapplication.GameData.onDraw(GameData.java:86) at android.view.View.draw(View.java:15231) at android.view.View.updateDisplayListIfDirty(View.java:14167) at android.view.View.getDisplayList(View.java:14189) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368) at android.view.View.updateDisplayListIfDirty(View.java:14127) at android.view.View.getDisplayList(View.java:14189) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368) at android.view.View.updateDisplayListIfDirty(View.java:14127) at android.view.View.getDisplayList(View.java:14189) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368) at android.view.View.updateDisplayListIfDirty(View.java:14127) at android.view.View.getDisplayList(View.java:14189) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368) at android.view.View.updateDisplayListIfDirty(View.java:14127) at android.view.View.getDisplayList(View.java:14189) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368) at android.view.View.updateDisplayListIfDirty(View.java:14127) at android.view.View.getDisplayList(View.java:14189) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318) at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:550) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
請確保你的對象不是null – Shailesh
目前還不清楚哪些代碼導致了錯誤,因爲你沒有包含[MCVE。能否請您閱讀您的堆棧跟蹤來驗證你的代碼崩潰 –
你的堆棧跟蹤告訴你到底爲什麼會得到一個NullPointerException ... – Krease