我从活动 A 开始一个活动 B。并且希望活动 A 被完全销毁并释放它所持有的所有内存,以便活动 B 开始时就好像它首先开始一样。我有一个按钮 onClick 方法,它完成了这个活动并使用从这里复制的方法 unbindDrawables 。
//inside activity A
public void onClickMethod(View v){
Intent intent = new Intent(this, EarthRoomActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);// starting activity B
unbindDrawables(findViewById(R.id.water_room_flipper));
this.finish();
}
static void unbindDrawables(View view) {
try{
System.out.println("UNBINDING"+view);
if (view.getBackground() != null) {
((BitmapDrawable)view.getBackground()).getBitmap().recycle();
view.getBackground().setCallback(null);
view=null;
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}}
但尽管如此,我收到了这个错误:
10-28 20:20:14 .220: E/AndroidRuntime(22709): FATAL EXCEPTION: main
10-28 20:20:14.220: E/AndroidRuntime(22709):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.room/com.example.room.EarthRoomActivity}:
android.view.InflateException: Binary XML file line #1: Error
inflating class <unknown> 10-28 20:20:14.220: E/AndroidRuntime(22709):
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.app.ActivityThread.access$600(ActivityThread.java:128) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.os.Handler.dispatchMessage(Handler.java:99) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.os.Looper.loop(Looper.java:137) 10-28 20:20:14.220:
E/AndroidRuntime(22709): at
android.app.ActivityThread.main(ActivityThread.java:4514) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
java.lang.reflect.Method.invokeNative(Native Method) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
java.lang.reflect.Method.invoke(Method.java:511) 10-28 20:20:14.220:
E/AndroidRuntime(22709): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
dalvik.system.NativeStart.main(Native Method) 10-28 20:20:14.220:
E/AndroidRuntime(22709): Caused by: android.view.InflateException:
Binary XML file line #1: Error inflating class <unknown> 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.createView(LayoutInflater.java:606) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.parseInclude(LayoutInflater.java:800)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.rInflate(LayoutInflater.java:729) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.rInflate(LayoutInflater.java:742) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.inflate(LayoutInflater.java:489) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.inflate(LayoutInflater.java:396) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.inflate(LayoutInflater.java:352) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:280)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.app.Activity.setContentView(Activity.java:1892) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
com.example.room.EarthRoomActivity.onCreate(EarthRoomActivity.java:28)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.app.Activity.performCreate(Activity.java:4562) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
10-28 20:20:14.220: E/AndroidRuntime(22709): ... 11 more 10-28
20:20:14.220: E/AndroidRuntime(22709): Caused by:
java.lang.reflect.InvocationTargetException 10-28 20:20:14.220:
E/AndroidRuntime(22709): at
java.lang.reflect.Constructor.constructNative(Native Method) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
java.lang.reflect.Constructor.newInstance(Constructor.java:417) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.LayoutInflater.createView(LayoutInflater.java:586) 10-28
20:20:14.220: E/AndroidRuntime(22709): ... 26 more 10-28
20:20:14.220: E/AndroidRuntime(22709): Caused by:
java.lang.OutOfMemoryError 10-28 20:20:14.220:
E/AndroidRuntime(22709): at
android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:577)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:775)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.content.res.Resources.loadDrawable(Resources.java:1968) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.content.res.TypedArray.getDrawable(TypedArray.java:601) 10-28
20:20:14.220: E/AndroidRuntime(22709): at
android.view.View.<init>(View.java:2857) 10-28 20:20:14.220:
E/AndroidRuntime(22709): at android.view.View.<init>(View.java:2794)
10-28 20:20:14.220: E/AndroidRuntime(22709): at
android.view.ViewGroup.<init>(ViewGroup.java:381) 10-28 20:20:14.220:
E/AndroidRuntime(22709): at
android.widget.RelativeLayout.<init>(RelativeLayout.java:174) 10-28
20:20:14.220: E/AndroidRuntime(22709): ... 29 more
我必须提到,当我从 android manifest 启动活动 B 使其成为第一个启动的应用程序时,它启动时没有错误。
活动 B xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_width="fill_parent" android:id="@+id/earth_room_layout"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ViewFlipper android:id="@+id/water_room_flipper"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<include layout="@layout/earth_room_wall1" android:id="@+id/first" />
<include layout="@layout/earth_room_wall2" android:id="@+id/second" />
<include layout="@layout/earth_room_wall3" android:id="@+id/third" />
<include layout="@layout/earth_room_wall4" android:id="@+id/fourth" />
</ViewFlipper>
<com.example.room.CustomView android:id="@+id/customView"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</FrameLayout>
如果我从 ViewFlipper 中删除最后一个,它可以工作。即它必须与 OutOfMemory 错误有关。但我确实释放了所有活动 A 持有的东西,不是吗?或者我可能错过了什么?