当我单击“主页”按钮或屏幕方向发生变化时,我的应用程序崩溃了,我尝试了许多解决方案,但没有一个起作用。我已经知道它可能与终止线程有关,但是我在编程中是新手,所以我将不胜感激。
这是代码和logcat。代码非常混乱,因为我尝试从该论坛和其他论坛中实施解决方案

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().hide();
    setContentView(new GameView(this));
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}


}`

public class GameView extends SurfaceView implements
            SurfaceHolder.Callback {


p   private KulaSprite kula;
private PaletkaSprite paletka;
private PaletkaSprite paletka2;
private GameLogic mGameLogic;
private boolean mGameIsRunning = false;
private int punkty = 0;
private String p1 = Integer.toString(punkty);
private int punkty2 = 0;
private String p2 = Integer.toString(punkty2);
private float speed;
Paint paint = new Paint();

public void start() {
    if (!mGameIsRunning) {
        mGameLogic.setGameState(GameLogic.RUNNING);
        mGameLogic.start();
        mGameIsRunning = true;
    } else {
        mGameLogic.onResume();
    }
}


    public GameView(Context context) {
        super(context);

        setFocusable(true);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        kula = new KulaSprite(BitmapFactory.decodeResource(getResources(), R.drawable.kula), 600,300);
        paletka = new PaletkaSprite(BitmapFactory.decodeResource(getResources(), R.drawable.paletka),15,height/2);
        paletka2 = new PaletkaSprite(BitmapFactory.decodeResource(getResources(), R.drawable.paletka2), width-15,height/2);
        mGameLogic = new GameLogic(getHolder(), this);
        getHolder().addCallback(this);
    }

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
}

    @Override
    public void surfaceCreated(SurfaceHolder holder){
        start();
    }


    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
    }

public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.rgb(00, 60, 10));
        paint.setTextSize((getHeight()*2)/3);
        paint.setColor(Color.rgb(00, 80, 10));
        canvas.drawText(p1, getWidth()/12, (getHeight()*3)/4,paint);
        canvas.drawText(p2, getWidth()/2, (getHeight()*3)/4,paint);
        kula.draw(canvas);
        paletka.draw(canvas);
        paletka2.draw(canvas);


    }




public class GameLogic extends Thread {

private SurfaceHolder _surfaceHolder;
private GameView mGameView;
private int game_state;
public static final int PAUSE = 0;
public static final int READY = 1;
public static final int RUNNING = 2;
private Object mPauseLock = new Object();
private boolean mPaused;


public GameLogic(SurfaceHolder surfaceHolder, GameView mGameView) {
    super();
    this._surfaceHolder = surfaceHolder;
    this.mGameView = mGameView;
}



public void setGameState(int gamestate) {
    this.game_state = gamestate;
}

public int getGameState(){
    return game_state;
}

public void onPause() {
    synchronized (mPauseLock) {
        mPaused = true;
    }
}

public void onResume() {
    synchronized (mPauseLock) {
        mPaused = false;
        mPauseLock.notifyAll();
    }
}

@Override
public void run() {

    Canvas canvas;
    canvas = null;

    while (game_state == RUNNING) {


        try {
                canvas = this._surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    this.mGameView.update();
                    this.mGameView.onDraw(canvas);

                }
        }

        finally {
                    if (canvas != null) {
                        _surfaceHolder.unlockCanvasAndPost(canvas);
                    }
        }
        synchronized (mPauseLock) {
            while (mPaused) {
                try {
                    mPauseLock.wait();
                } catch (InterruptedException e) {
                }
            }
        }
    }
}


}

03-30 14:19:24.880: D/libEGL(3010): loaded /system/lib/egl/libEGL_tegra.so
03-30 14:19:24.910: D/libEGL(3010): loaded /system/lib/egl/libGLESv1_CM_tegra.so
03-30 14:19:24.920: D/libEGL(3010): loaded /system/lib/egl/libGLESv2_tegra.so
03-30 14:19:24.960: D/OpenGLRenderer(3010): Enabling debug mode 0
03-30 14:19:29.270: W/dalvikvm(3010): threadid=11: thread exiting with uncaught         exception (group=0x41703ba8)
03-30 14:19:29.270: E/AndroidRuntime(3010): FATAL EXCEPTION: Thread-2715
03-30 14:19:29.270: E/AndroidRuntime(3010): Process: com.pingpong, PID:  3010
03-30 14:19:29.270: E/AndroidRuntime(3010): java.lang.NullPointerException
03-30 14:19:29.270: E/AndroidRuntime(3010):     at       com.GameView.onDraw(GameView.java:88)
03-30 14:19:29.270: E/AndroidRuntime(3010):     at   com.pingpong.GameLogic.run(GameLogic.java:60)

最佳答案

按下后退按钮或旋转设备将使活动重新创建。由于您正在制作游戏,因此将屏幕锁定到该活动的AndroidManifest文件中所需的方向上,以便方向更改不会触发它。
http://developer.android.com/guide/topics/manifest/activity-element.html

要处理用户退出(后退或主页按下)的操作,您需要覆盖活动中的onPause()onStop()并停止执行游戏循环。由于它们位于单独的线程中,因此无论您是否处于活动中,循环都将继续执行。

10-08 10:59