0

I start an activity B from activity A. And want activity A to be completely destroyed and all the memory it is holding to be released, so that activity B starts as if it is starting first. I have a button onClick method, which finishes this activity and uses the method unbindDrawables copied from here.

//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();
}}   

But nevertheles I'm getting this error:

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

I have to mention that when I start activity B from android manifest making it the first app to be launched, it launches without errors.

Activity 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>

If I remove last from the ViewFlipper it works. I.e. it has to do smth with OutOfMemory error. But I did release everything activity A was holding, didn't I? Or may be I missed smth?

Community
  • 1
  • 1
Nazerke
  • 2,098
  • 7
  • 37
  • 57
  • `android.view.InflateException: Binary XML file line #1: Error` Looks like the problem is with your `XML`, which you didn't post. – nhgrif Oct 28 '13 at 14:44
  • I've added it to the edit. please have a look. – Nazerke Oct 28 '13 at 14:50
  • just as a comment, [FrameLayouts](http://developer.android.com/reference/android/widget/FrameLayout.html) should not be used normally for storing more than one child view. – Jorge Aguilar Oct 28 '13 at 14:57
  • @Jorge how else can I make custom view appear on top of all the layouts included in viewflipper? – Nazerke Oct 28 '13 at 14:59
  • 1
    you can use a relativelayout and put it on top, check [this](http://stackoverflow.com/questions/2614393/defining-z-order-of-views-of-relativelayout-in-android) answer to better understand what i said. – Jorge Aguilar Oct 28 '13 at 15:02
  • @JorgeAguilar thank you. why more than one child view should not be used in frame layout? also, could that be a reason of my error? – Nazerke Oct 28 '13 at 15:05
  • check the link in my first comment, and you can see it on the documentation, first line. But it also says that is possible to add more than one, so i do not think that is part of your error, but you can try, and see what happens. – Jorge Aguilar Oct 28 '13 at 15:08

1 Answers1

0

There seems to be something wrong with the layout of Activity B, it seems the error is in line #1, maybe you forgot to add

<?xml version="1.0" encoding="utf-8"?>

at the top. If you post the layout I can help you further.

Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86