gpt4 book ai didi

java - 更改可见性导致我的应用程序崩溃,为什么?

转载 作者:行者123 更新时间:2023-12-02 09:42:12 25 4
gpt4 key购买 nike

我有两个类。一个类在我的 GameView 类中创建了我的迷宫游戏,该类扩展了 View。 GameView 类:将我的 View 的可见性设置为可见,直到玩家 == 退出,然后它变得不可见(按预期),但我的 LinearLayout View (在 OnCreate、MainActivity.java 中为不可见)设置为可见,当 boolean 值 (player == exit) 为 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.

最佳答案

我已经看过您关于此的其他帖子,并且认为问题是因为您尝试访问与该类不相关的布局对象,您可以使用 setContentView 在 onCreate() 的主类中执行此操作(R.layout.activity_main);关联它并通过您的其他类的引用发送它,但我不确定传递它是否会起作用。

关于java - 更改可见性导致我的应用程序崩溃,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56975302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com