有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有两门课。有一个类在GameView类中创建了我的迷宫游戏,它扩展了View。GameView类:将我的视图的可见性设置为在player==exit之前可见,然后它变为不可见(如预期),但当布尔值(player==exit)为true时,设置为可见的我的LinearLayout视图(在创建MainActivity.java时不可见)会导致应用程序崩溃

我知道更改LinearLayout的可视性会导致崩溃,就好像我把它注释掉,它会使应用程序崩溃一样。我试图改变MainActivity类中的可见性,但这似乎也不起作用

我目前正在尝试使用Intent将此信息推送到MainActivity,使用我的PlayReach(),但我不确定如何操作

@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);

        }
    }

我想要的是让迷宫变得不可见,线性布局变得可见,并允许用户点击我的按钮(现在重启迷宫)

堆栈跟踪(?)

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.practicing安卓withsean.mazeone, PID: 1319
    java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.widget.LinearLayout.setVisibility(int)' on a null object reference
        at com.example.practicing安卓withsean.mazeone.GameView.checkExit(GameView.java:286)
        at com.example.practicing安卓withsean.mazeone.GameView.movePlayer(GameView.java:274)
        at com.example.practicing安卓withsean.mazeone.GameView.onTouchEvent(GameView.java:341)
        at 安卓.view.View.dispatchTouchEvent(View.java:12540)
        at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3159)
        at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2844)
        at com.安卓.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:601)
        at com.安卓.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1871)
        at 安卓.app.Activity.dispatchTouchEvent(Activity.java:3384)
        at 安卓x.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.安卓.tools.profiler.support.event.WindowProfilerCallback.dispatchTouchEvent(WindowProfilerCallback.java:69)
        at com.安卓.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:563)
        at 安卓.view.View.dispatchPointerEvent(View.java:12788)
        at 安卓.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5670)
        at 安卓.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5465)
        at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4958)
        at 安卓.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5011)
        at 安卓.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4977)
        at 安卓.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5114)
        at 安卓.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4985)
        at 安卓.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5171)
        at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4958)
        at 安卓.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5011)
        at 安卓.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4977)
        at 安卓.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4985)
        at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4958)
        at 安卓.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7736)
        at 安卓.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7676)
        at 安卓.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7637)
        at 安卓.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7847)
        at 安卓.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:197)
        at 安卓.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
        at 安卓.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:186)
        at 安卓.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7810)
        at 安卓.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7874)
        at 安卓.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at 安卓.view.Choreographer.doCallbacks(Choreographer.java:723)
        at 安卓.view.Choreographer.doFrame(Choreographer.java:652)
E/AndroidRuntime:     at 安卓.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at 安卓.os.Handler.handleCallback(Handler.java:789)
        at 安卓.os.Handler.dispatchMessage(Handler.java:98)
        at 安卓.os.Looper.loop(Looper.java:164)
        at 安卓.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.安卓.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Application terminated.

共 (3) 个答案

  1. # 1 楼答案

    我看过你关于这一点的另一篇帖子,我认为问题是因为你试图访问一个与该类无关的布局对象,你可以使用setContentView(R.layout.activity_main)在onCreate()上的主类中访问它;把它联系起来,通过其他课程的参考发送给你,但我不确定通过它是否有效

  2. # 2 楼答案

    我想你忘了gameView = findViewByID(R.id.your_gameview_id)

  3. # 3 楼答案

    您没有定义gameView变量。 你应该把 gameView = findeVideById(R.id.game_view) 在onCreate方法中