Android 4.3中的android.graphics.BitmapFactory.nativeDecodeAsset问题

时间:2014-03-10 20:03:17

标签: android

我的应用只有Android 4.3有问题。我不确定是什么原因以及错误的来源。基本上我没有使用任何我可以调试它的位图函数。以下是Google Play的错误日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.capripio.wordzcatch/com.capripio.wordzcatch.SplashActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
            at android.app.ActivityThread.access$700(ActivityThread.java:168)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5493)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
            at dalvik.system.NativeStart.main(Native Method)
            Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
            at android.view.LayoutInflater.createView(LayoutInflater.java:626)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
            at android.app.Activity.setContentView(Activity.java:1956)
            at com.capripio.wordzcatch.SplashActivity.onCreate(SplashActivity.java:14)
            at android.app.Activity.performCreate(Activity.java:5372)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
            ... 11 more
            Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:600)
            ... 23 more
            Caused by: java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
            at android.content.res.Resources.loadDrawable(Resources.java:2988)
            at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
            at android.view.View.<init>(View.java:3563)
            at android.view.View.<init>(View.java:3492)
            at android.view.ViewGroup.<init>(ViewGroup.java:469)
            at android.widget.RelativeLayout.<init>(RelativeLayout.java:242)
            ... 26 more

谢谢!

3 个答案:

答案 0 :(得分:2)

我已经找到了办法做小班制。

public abstract class BitmapResLoader {

public static Bitmap decodeBitmapFromResource(Bitmap.Config config, Resources res, int resId, int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = config;

    return BitmapFactory.decodeResource(res, resId, options);
}

private static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}
}

并将其用作

decodeBitmapFromResource(
            Bitmap.Config.ARGB_8888, splashActivity.getResources(),
            R.drawable.bg, 480, 800);

答案 1 :(得分:0)

尝试放:

机器人:largeHeap =&#34;真&#34;在您的清单申请标签

答案 2 :(得分:-1)

如果你看一下

Caused by: java.lang.OutOfMemoryError

表示错误是由java.lang.OutOfMemoryError

引起的