我使用了一个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);
}
崩溃logcat是
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)
注意:此次崩溃发生在第二次,但第三次崩溃,第四次崩溃,五次正常等等。
答案 0 :(得分: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();
}
}
答案 1 :(得分: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);
}
现在,一切进展顺利,谢谢!