我正在开发一款安卓游戏,并且刚刚添加了一个主菜单。这是一个单独的活动——所以当有人点击菜单上的“新游戏”按钮时,它会打开一个新的游戏活动意图。
我的问题是如果用户点击“返回”,它会关闭游戏并返回主菜单。当我再次点击“新游戏”时,它崩溃了。
我认为它正在尝试制作游戏的第二个版本,并且内存不足。我是否应该尝试在菜单中保留游戏活动的副本,然后在他们再次点击“新游戏”时重新打开它?或者有没有更简单的方法来做到这一点?
我的“新游戏”按钮侦听器执行以下操作:(IslandGame 是我的主要游戏类 - 捆绑只是告诉它是开始新游戏还是加载以前的游戏,以及从哪个级别开始)
ButtonNewGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString("RESTORE", "NEW"); //in the restore key, say we want a new game
bundle.putInt("CAMPAIGN", 1); //start in campaign #1
Intent intent = new Intent(Intent.ACTION_RUN);
intent.putExtras(bundle);
intent.setClassName(thisActivity, IslandGame.class.getName());
startActivity(intent);
}
});
日志是:
06-09 17:42:34.199: E/dalvikvm-heap(239): 147456-byte external allocation too large for this process.
06-09 17:42:34.199: E/(239): VM won't let us allocate 147456 bytes
06-09 17:42:34.209: D/AndroidRuntime(239): Shutting down VM
06-09 17:42:34.209: W/dalvikvm(239): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-09 17:42:34.219: E/AndroidRuntime(239): Uncaught handler: thread main exiting due to uncaught exception
06-09 17:42:34.249: E/AndroidRuntime(239): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dylan.island/com.dylan.island.IslandGame}: android.view.InflateException: Binary XML file line #37: Error inflating class <unknown>
06-09 17:42:34.249: E/AndroidRuntime(239): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.os.Looper.loop(Looper.java:123)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-09 17:42:34.249: E/AndroidRuntime(239): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 17:42:34.249: E/AndroidRuntime(239): at java.lang.reflect.Method.invoke(Method.java:521)
06-09 17:42:34.249: E/AndroidRuntime(239): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-09 17:42:34.249: E/AndroidRuntime(239): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-09 17:42:34.249: E/AndroidRuntime(239): at dalvik.system.NativeStart.main(Native Method)
06-09 17:42:34.249: E/AndroidRuntime(239): Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class <unknown>
06-09 17:42:34.249: E/AndroidRuntime(239): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-09 17:42:34.249: E/AndroidRuntime(239): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.app.Activity.setContentView(Activity.java:1622)
06-09 17:42:34.249: E/AndroidRuntime(239): at com.dylan.island.IslandGame.onCreate(IslandGame.java:76)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-09 17:42:34.249: E/AndroidRuntime(239): ... 11 more
06-09 17:42:34.249: E/AndroidRuntime(239): Caused by: java.lang.reflect.InvocationTargetException
06-09 17:42:34.249: E/AndroidRuntime(239): at com.dylan.island.IslandView.<init>(IslandView.java:1983)
06-09 17:42:34.249: E/AndroidRuntime(239): at java.lang.reflect.Constructor.constructNative(Native Method)
06-09 17:42:34.249: E/AndroidRuntime(239): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
06-09 17:42:34.249: E/AndroidRuntime(239): ... 23 more
06-09 17:42:34.249: E/AndroidRuntime(239): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-09 17:42:34.249: E/AndroidRuntime(239): at android.graphics.Bitmap.nativeCreate(Native Method)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:346)
06-09 17:42:34.249: E/AndroidRuntime(239): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:372)
06-09 17:42:34.249: E/AndroidRuntime(239): at com.dylan.island.IslandView$IslandThread.initimages(IslandView.java:435)
06-09 17:42:34.249: E/AndroidRuntime(239): at com.dylan.island.IslandView$IslandThread.beginLevel(IslandView.java:220)
06-09 17:42:34.249: E/AndroidRuntime(239): at com.dylan.island.IslandView$IslandThread.<init>(IslandView.java:215)
06-09 17:42:34.249: E/AndroidRuntime(239): ... 27 more