1
當我點擊主頁按鈕或當屏幕方向正在改變時,我的應用程序崩潰了我已經嘗試了很多解決方案,但都沒有工作。我已經知道它可能與終止線程連接,但即時通訊編程新,所以我將不勝感激任何幫助。 這是代碼和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)
一些通用筆記在這裏:http://stackoverflow.com/questions/21567641/should-the-renderingthread-of-a-surfaceview-have-the-same-life-cycle-as-the-view/21684399 #21684399 – fadden