获取内存不足的错误,同时开始在Android应用程序一个活动? [英] Getting out of memory error while starting a Activity in android App?

查看:107
本文介绍了获取内存不足的错误,同时开始在Android应用程序一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,其中一个活动是一个非常复杂的code与膨胀在这样的WebView一个布局的不同的看法(这是从SD卡与图像使用HTML文件),imageviews ,scrolls.At启动该活动我得到这些错误。

I am working on an app where one activity is a very complex code with different views inflated in a single layout like webview(which is using html files from sdcard with images),imageviews,scrolls.At starting this Activity I am getting these errors.

        E/AndroidRuntime(600): Caused by: java.lang.reflect.InvocationTargetException
        E/AndroidRuntime(600):  at java.lang.reflect.Constructor.constructNative(Native Method)
        E/AndroidRuntime(600):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
        E/AndroidRuntime(600):  at android.view.LayoutInflater.createView(LayoutInflater.java:586)
        E/AndroidRuntime(600):  ... 29 more
        E/AndroidRuntime(600): Caused by: java.lang.OutOfMemoryError
        E/AndroidRuntime(600):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        E/AndroidRuntime(600):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
        E/AndroidRuntime(600):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
        E/AndroidRuntime(600):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
        E/AndroidRuntime(600):  at android.content.res.Resources.loadDrawable(Resources.java:1935)
        E/AndroidRuntime(600):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
        E/AndroidRuntime(600):  at android.view.View.<init>(View.java:2785)
        E/AndroidRuntime(600):  at android.view.View.<init>(View.java:2722)
        E/AndroidRuntime(600):  at android.view.ViewGroup.<init>(ViewGroup.java:379)
        E/AndroidRuntime(600):  at android.widget.RelativeLayout.<init>(RelativeLayout.java:174)
        E/AndroidRuntime(600):  ... 32 more

如何解决这个问题我已经试过这code,但还是不知道该怎么办?

How to solve this problem I have tried this code but still not knowing what to do?

 @Override
    protected void onDestroy() {
    super.onDestroy();

    unbindDrawables(findViewById(R.id.rootView));
    System.gc();
    }

    private void unbindDrawables(View view) {
        if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
        ((ViewGroup) view).removeAllViews();
        }
    }

什么是解决这一问题的可能途径?请帮助

What is the possible way to solve this problem? Please Help

推荐答案

优化位图的内存使用的情况下,最好的办法就是保存ImageViews列表使这个为每个ImageView的调用完成()之前:

The best way to optimize memory usage with bitmaps in your case is to save a list of your ImageViews make this before call finish() for every ImageView:

((BitmapDrawable) myImageView.getDrawable()).getBitmap().recycle();
myImageView = null;

使用过的code。在的onPause code,与具有ID的活动的主要布局 top_layout

Use too the code in onPause code, with the main layout of your activity having the id top_layout:

@Override
protected void onPause() {
    super.onPause();
    unbindDrawables(findViewById(R.id.top_layout));
    System.gc();
}

这篇关于获取内存不足的错误,同时开始在Android应用程序一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆