简体   繁体   中英

Android setContentView crashes my application

I have made a public void method in my MainActivity to switch to another layout but whenever I call this method, the app crashes.

logcat:

07-05 16:35:22.823 14683-14712/com.example.name.mygame I/OpenGLRenderer: Initialized EGL, version 1.4

[ 07-05 16:35:24.724 14683:14754 D/]
HostConnection::get() New Host Connection established 0xae4b2240, tid 14754
07-05 16:35:28.500 14683-14754/com.example.name.mygame I/System.out: gameOverUi called!
-------- beginning of crash
07-05 16:35:28.558 14683-14754/com.example.name.mygame E/AndroidRuntime: FATAL EXCEPTION: Thread-218
Process: com.example.name.mygame, PID: 14683
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.Window.setContentView(int)' on a null object reference
at android.app.Activity.setContentView(Activity.java:2166)
at com.example.name.mygame.MainActivity.gameOverUi(MainActivity.java:15)
at com.example.name.mygame.GameView.gameOver(GameView.java:16)
at com.example.name.mygame.GameView.onDraw(GameView.java:188)
at com.example.name.mygame.GameLoopThread.run(GameLoopThread.java:37)
07-05 16:35:30.070 14683-14754/com.example.name.mygame I/Process: Sending signal. PID: 14683 SIG: 9

MainActivity:

package com.example.name.mygame;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GameView(this));
    }

    public void gameOverUi(){
        System.out.println("gameOverUi called!");
        setContentView(R.layout.gameover); //Crash here
    }
}

GameView class:

package com.example.name.mygame;

import android.content.Context;
import android.view.SurfaceView;

public class GameView extends SurfaceView {
    MainActivity mActivity = new MainActivity();

        public GameView(Context context) {
        super(context);
        //unimportant stuff
    }


    private void gameOver(){
        mActivity.gameOverUi();
    }
}

How can I change the content view without causing my application to crash? Any answers would be appreciated, thanks.

MainActivity mActivity = new MainActivity();

This is your problem.

This way you create a new allocation of a MainActivity, but you don't reference your MainActivity. For this, you have a null point exception.

Avoid this.

You are not referencing your MainActivity class correctly within the GameView - as you are currently creating a new object and not getting a reference to your Activity that is being shown. This is not the correct way to access Activities.

As Activity inherits from Context you could theoretically do something like this:

public class GameView extends SurfaceView {
    private MainActivity mActivity;

    //Create new constructor to get the Activity
    public GameView(Activity activity) {
        super(activity);
        mActivity = (MainActivity) activity;
    }

    private void gameOver(){
        mActivity.gameOverUi();
    }
} 

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