0

我使用 contentView “gameView” 它扩展了 SurfaceView 并且我使用线程对我的游戏进行永久循环,我的 onCreate() 方法是

public class mainclass extends Activity {
GameView game;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    game = new GameView(this);
    setContentView(game);
}

崩溃日志猫是

05-09 09:35:06.378: W/dalvikvm(9664): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-09 09:35:06.388: E/AndroidRuntime(9664): FATAL EXCEPTION: main
05-09 09:35:06.388: E/AndroidRuntime(9664): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:359)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:385)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at superBill.Mbiz.Egypt.GameView.<init>(GameView.java:87)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at superBill.Mbiz.Egypt.SuperBillActivity.onCreate(SuperBillActivity.java:20)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.os.Looper.loop(Looper.java:130)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at java.lang.reflect.Method.invoke(Method.java:507)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-09 09:35:06.388: E/AndroidRuntime(9664):     at dalvik.system.NativeStart.main(Native Method)

注意:此崩溃发生在第二次,但第三次正常,第四次崩溃,第五次正常,依此类推。

4

2 回答 2

1

在 GameView 的 surfaceCreated 方法中,您需要重新创建游戏线程,因为您可能会在 surfaceDestroyed 方法中使用 gamethread.join() 让它死掉。像这样的东西:

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (gamethread.getState() == Thread.State.TERMINATED) {
        gamethread = new GameThread(getHolder(), this);
        gamethread.setRunning(true);
        gamethread.start();
    } else if (gamethread.getState() == Thread.State.NEW) {
        gamethread.setRunning(true);
        gamethread.start();
    }
}
于 2012-05-08T15:17:42.550 回答
0

@dymmeh:我的代码是

@Override
          public void surfaceDestroyed(SurfaceHolder holder) 
        {
            boolean retry = true;
            mythread.setRunning(false);
            while (retry) {

                   try {
                            mythread.join();
                            retry = false;
                       } catch (InterruptedException e) {
                   }
            }
     }

那是什么导致了你所说的,我把它替换为

    @Override
          public void surfaceDestroyed(SurfaceHolder holder) 
        {
            mythread.setRunning(false);
            System.exit(0);
        }

现在,一切顺利,谢谢!

于 2012-05-10T07:49:41.603 回答