开发者

android.view.InflateException: Binary XML <Unknown>, and OutOfMemoryError?

I am, using nearly 90 very big images,and i am using lazy loading for displaying that images in Gallery,and grid view.so after loading the images ,if you click on any button,it giving outofmemory(oom) error,and sometimes it giving Xml binary inflate exception.

I am using very big images,equal to tablet size images,and my apk size is 25MB,.and i am using single activity with multiple FrameLayouts but before loading new View I am removing current view but still problem is there,I dont know why it is happening ?Its giving the oom error but its not giving the exact location of error,and its giving error at random position and error coming frequently.

09-30 15:23:07.490: ERROR/AndroidRuntime(5527): FATAL EXCEPTION: main
09-30 15:23:07.490: ERROR/AndroidRuntime(5527): android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.view.LayoutInflater.createView(LayoutInflater.java:596)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:644)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at com.maheshbabu.app.JustIn.initialize(JustIn.java:143)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at com.maheshbabu.app.JustIn.<init>(JustIn.java:135)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at com.maheshbabu.app.MaheshBabuHomePage.showLatestPage(MaheshBabuHomePage.java:671)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at com.maheshbabu.app.MaheshBabuHomePage.onClick(MaheshBabuHomePage.java:363)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.view.View.performClick(View.java:3110)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.view.View$PerformClick.run(View.java:11934)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.os.Handler.handleCallback(Handler.java:587)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.os.Looper.loop(Looper.java:132)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.app.ActivityThread.main(ActivityThread.java:4123)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at java.lang.reflect.Method.invoke(Method.java:491)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at dalvik.system.NativeStart.main(Native Method)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527): Caused by: java.lang.reflect.InvocationTargetException
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at java.lang.reflect.Constructor.constructNative(Native Method)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at java.lang.reflect.Constructor.newInstance(Constructor.java:416)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.view.LayoutInflater.createView(LayoutInflater.java:576)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     ... 21 more
09-30 15:23:07.490: ERROR/AndroidRuntime(5527): Caused by: java.lang.OutOfMemoryError
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:738)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.content.res.Resources.loadDrawable(Resources.java:1918)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-30 15:23:07.4开发者_运维百科90: ERROR/AndroidRuntime(5527):     at android.view.View.<init>(View.java:2450)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.view.View.<init>(View.java:2389)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.view.ViewGroup.<init>(ViewGroup.java:359)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     at android.widget.RelativeLayout.<init>(RelativeLayout.java:173)
09-30 15:23:07.490: ERROR/AndroidRuntime(5527):     ... 24 more


I think this exception is comming from

setContentView(R.layout.main);

line this may be because of various reasons.

1) Because you are changing your screen orientation from potrait to landscape and vice versa

Sol) Fix your view to any one orientation. Actually on orientation change new views and new objects are created along with old view and object . thus taking your memory

2) Objects are not freed

sol) Removing views will not solve your problem you have to free all the objects related to those views. Call Bitmap.recycle() on every image related to those views.

also check out these links which may be helpful to you

How to get rid of out of memory exception

Check out this video which may be very useful for you

Memory management for Android Apps

Happy coding friend


I have solved it in a tricky way.

I have two layout 1) layout_splash : layout of Splash 2) layout_map: Layout which contain map fragment and which cause crash;

In onCreate of My first activity (Launching Activity - SplashActivity.java) I have done like the following.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        if(close){
            finish();
            System.exit(0);
        }

        setContentView(R.layout.layout_map);
        setContentView(R.layout.layout_splash);

        .......
        .......
        .......

    } catch (Exception e) {
        Intent intent = new Intent(SplashScreenActivity.this,LowMemoryDialogActivity.class);
        startActivity(intent);
        //The below code will prevent showing crash dialog.
        System.exit(1);
    }
}


LowMemoryDialogActivity.java

public class LowMemoryDialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_low_memory_dialog);
    }

    @Override
    public void onBackPressed() {
        SplashScreenActivity.close=true;
        super.onBackPressed();
    }
}

activity_low_memory_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Low Memory"
        android:id="@+id/textView6"
        android:layout_above="@+id/textView5"
        android:layout_centerHorizontal="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please insert SD card for proper working!"
        android:id="@+id/textView5"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</LinearLayout>

by inserting setContentView(R.layout.layout_map) first time in a try catch, we can understand the possibilities of future crash and we can handle that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜