无法在Android

时间:2016-08-20 14:16:33

标签: java android

我正在写游戏,我需要在游戏结束时切换活动。我的问题是我有一个gameLogic Java类,它扩展到Activity,它不是一个主要的活动类。

所以我在GameLogic Class中有一个gameover方法,它返回boolean。如果它返回false,则必须切换到第二个Activity,但它不会。它返回null.pointer.exception

以下是代码:

public void gameOver(GameData data){

        if(!intersect(data)){

            Intent intent = new Intent(GameLogic.this , SecondActivity.class);
            startActivity(intent);

        }
}

我在OnDraw方法的GameData类中调用此方法,因为OnDraw方法总是必须控制它是否是游戏结束

protected void onDraw(Canvas canvas){
.........
....
......
.....
............
logic.gameOver(this);

}

ERROR:

  

08-20 14:13:58.293 29830-29830 / com.example.msimsi.myapplication   E / AndroidRuntime:FATAL EXCEPTION:main

     

处理:com.example.msimsi.myapplication,PID:29830
  java.lang.NullPointerException:尝试调用虚方法

     

' java.lang.String android.content.Context.getPackageName()'在null   对象参考                                                                                             在   android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)                                                                                             在android.content.ComponentName。(ComponentName.java:77)                                                                                             在android.content.Intent。(Intent.java:4160)                                                                                             在   com.example.msimsi.myapplication.GameLogic.gameOver(GameLogic.java:230)                                                                                             at com.example.msimsi.myapplication.GameData.onDraw(GameData.java:86)                                                                                             在android.view.View.draw(View.java:15231)                                                                                             在android.view.View.updateDisplayListIfDirty(View.java:14167)                                                                                             在android.view.View.getDisplayList(View.java:14189)                                                                                             在   android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)                                                                                             在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)                                                                                             在android.view.View.updateDisplayListIfDirty(View.java:14127)                                                                                             在android.view.View.getDisplayList(View.java:14189)                                                                                             在   android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)                                                                                             在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)                                                                                             在android.view.View.updateDisplayListIfDirty(View.java:14127)                                                                                             在android.view.View.getDisplayList(View.java:14189)                                                                                             在   android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)                                                                                             在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)                                                                                             在android.view.View.updateDisplayListIfDirty(View.java:14127)                                                                                             在android.view.View.getDisplayList(View.java:14189)                                                                                             在   android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)                                                                                             在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)                                                                                             在android.view.View.updateDisplayListIfDirty(View.java:14127)                                                                                             在android.view.View.getDisplayList(View.java:14189)                                                                                             在   android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)                                                                                             在android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)                                                                                             在android.view.View.updateDisplayListIfDirty(View.java:14127)                                                                                             在android.view.View.getDisplayList(View.java:14189)                                                                                             在   android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)                                                                                             在   android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)                                                                                             在android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)                                                                                             在android.view.ViewRootImpl.draw(ViewRootImpl.java:2530)                                                                                             在android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)                                                                                             在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)                                                                                             在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)                                                                                             在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5885)                                                                                             在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)                                                                                             在android.view.Choreographer.doCallbacks(Choreographer.java:580)                                                                                             在android.view.Choreographer.doFrame(Choreographer.java:550)                                                                                             在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)                                                                                             在android.os.Handler.handleCallback(Handler.java:739)                                                                                             在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                             在android.os.Looper.loop(Looper.java:135)                                                                                             在android.app.ActivityThread.main(ActivityThread.java:5254)                                                                                             at java.lang.reflect.Method.invoke(Native Method)                                                                                             在java.lang.reflect.Method.invoke(Method.java:372)                                                                                             在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)                                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:0)

您需要获取应用程序上下文。

*current-mode-var*

我不是专家。