简体   繁体   中英

Changing visibility is causing my app to crash, why?

I have 2 classes. One class has created my maze game in my GameView class which extends View. The GameView class : sets my view's visibility to be VISIBLE until player == exit, then it goes invisible (as intended) but my LinearLayout view (which is INVISIBLE OnCreate, MainActivity.java) which is set to go VISIBLE when the boolean (player == exit) is true, causes for the app to crash.

I know changing the LinearLayout's visiblity is causing the crash as if I comment it out, it crashes the app. I've tried to change the visibility's in my MainActivity class but that doesn't seem to be working either.

I'm currently trying to use Intent to push this information to the MainActivity, to use my playAgain() but I'm not sure how to do it.

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

        }
    }

What I want is for the maze to go INVISIBLE, the LinearLayout to become VISIBLE and allow for the user to be able to tap my button (to restart the maze, for now).

Stack Trace(?):

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.

I have seen your other post about that, and think that the problem is because you try to acces a layout object that is not related with that class, you can do it in your main class on your onCreate() by using setContentView(R.layout.activity_main); to asociate it and send it via reference on your other class, but I'm not sure that with passing it is going to work.

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

You didn't define gameView variable. You should put gameView = findeVideById(R.id.game_view) in your onCreate method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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