简体   繁体   English

Android setContentView使我的应用程序崩溃

[英]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. 我在MainActivity中创建了一个public void方法,以切换到其他布局,但是每当我调用此方法时,应用程序就会崩溃。

logcat: 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: GameView类:

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. 这样,您可以创建MainActivity的新分配,但不会引用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. 你是不是引用您的MainActivity正确类内GameView -当你正在创建一个新的对象,而不是让你一个参考Activity内容显示。 This is not the correct way to access Activities. 这不是访问活动的正确方法。

As Activity inherits from Context you could theoretically do something like this: 由于ActivityContext继承的,因此理论上您可以执行以下操作:

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();
    }
} 

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

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