我从活动A开始活动B.并且希望活动A被完全销毁并且它所持有的所有内存都被释放,因此活动B就像它首先开始一样开始。我有一个按钮onClick方法,它完成此活动并使用从here复制的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();
}}
但是nevertheles我收到了这个错误:
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清单启动活动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错误做smth。但是我确实释放了A所持有的所有活动,不是吗?或者可能是我错过了什么?
答案 0 :(得分:0)
活动B的布局似乎有问题,似乎错误在第1行,也许你忘了添加
<?xml version="1.0" encoding="utf-8"?>
在顶部。如果您发布布局,我可以进一步帮助您。