繁体   English   中英

更改可见性会导致我的应用崩溃,为什么?

[英]Changing visibility is causing my app to crash, why?

我有2节课。 一个类在我的GameView类中创建了我的迷宫游戏,该类扩展了View。 GameView类:将我的视图的可见性设置为VISIBLE,直到玩家==退出,然后变为不可见(按预期),但是我的LinearLayout视图(这是INVISIBLE OnCreate,MainActivity.java)设置为当布尔值(玩家)变为可见时==出口)为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(R)在onCreate()的主类中完成此操作。 layout.activity_main); 关联它,并通过其他类上的引用发送它,但是我不确定通过它是否会起作用。

我认为您忘记了gameView = findViewByID(R.id.your_gameview_id)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM