我有2节课。一个类在我的GameView类中创建了我的迷宫游戏,该类扩展了View。 GameView类:将我的视图的可见性设置为VISIBLE,直到玩家==退出为止,然后变为不可见(按预期),但是我的LinearLayout视图(这是INVISIBLE OnCreate,MainActivity.java)设置为当布尔值(玩家)进入VISIBLE ==出口)为true,导致应用崩溃。
我知道更改LinearLayout的可见性会导致崩溃,就像我将其注释掉一样,它会使应用程序崩溃。我试图在MainActivity类中更改可见性,但这似乎也不起作用。
我目前正在尝试使用Intent将这些信息推送到MainActivity,以使用我的playAgain(),但是我不确定该怎么做。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = findViewById(R.id.playAgainLayout);
layout.setVisibility(View.INVISIBLE);
}
public void createMaze(){
...
...
do {
next = getNeighbour(current);
if (next != null) {
removeWall(current, next);
stack.push(current);
current = next;
current.visited = true;
} else
current = stack.pop(); //gives us the top element of the stack and removes it from the
}while(!stack.empty());
Animation slideUp = AnimationUtils.loadAnimation(getContext(), R.anim.slide_up);
gameView.startAnimation(slideUp);
}
public void checkExit() {
if (player == exit && counter < 3) {
counter++;
Animation slideDown = AnimationUtils.loadAnimation(getContext(), R.anim.mazeslidedown);
gameView.startAnimation(slideDown);
gameView.setVisibility(INVISIBLE);
// layout.setVisibility(VISIBLE);
}
}
我要使迷宫变得不可见,使LinearLayout变为可见,并允许用户点击我的按钮(暂时重新启动迷宫)。
堆栈跟踪(?):
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.practicingandroidwithsean.mazeone, PID: 1319
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setVisibility(int)' on a null object reference
at com.example.practicingandroidwithsean.mazeone.GameView.checkExit(GameView.java:286)
at com.example.practicingandroidwithsean.mazeone.GameView.movePlayer(GameView.java:274)
at com.example.practicingandroidwithsean.mazeone.GameView.onTouchEvent(GameView.java:341)
at android.view.View.dispatchTouchEvent(View.java:12540)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:601)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1871)
at android.app.Activity.dispatchTouchEvent(Activity.java:3384)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.tools.profiler.support.event.WindowProfilerCallback.dispatchTouchEvent(WindowProfilerCallback.java:69)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:563)
at android.view.View.dispatchPointerEvent(View.java:12788)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5670)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5465)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4958)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5011)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4977)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5114)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4985)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5171)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4958)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5011)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4977)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4985)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4958)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7736)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7676)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7637)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7847)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:197)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:186)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7810)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7874)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:652)
E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Application terminated.
答案 0 :(得分:0)
我认为您忘记了gameView = findViewByID(R.id.your_gameview_id)
答案 1 :(得分:0)
我看过另一篇关于此的文章,并认为问题是因为您尝试访问与该类无关的布局对象,因此可以在您的主类中通过使用setContentView来实现它(R.layout.activity_main);关联它并通过其他班级上的引用发送它,但是我不确定通过它是否会起作用。
答案 2 :(得分:0)
您没有定义gameView变量。
你应该放
gameView = findeVideById(R.id.game_view)
在您的onCreate方法中