• bug_ _图片_android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>


    =========== 1   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zgan.community/com.zgan.community.activity.CommunityPolicitalDetailActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>

     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
      at android.app.ActivityThread.access$600(ActivityThread.java:130) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 

     Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown> 
      at android.view.LayoutInflater.createView(LayoutInflater.java:613) 
      at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
      at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660) 
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685) 
     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:587) 
      ... 23 more 
     Caused by: java.lang.OutOfMemoryError 
      at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500) 
      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353) 
      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781) 
      at android.content.res.Resources.loadDrawable(Resources.java:2057) 
      at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
      at android.view.View.<init>(View.java:3336) 
      at android.view.ViewGroup.<init>(ViewGroup.java:427) 
      at android.widget.LinearLayout.<init>(LinearLayout.java:176) 
      at android.widget.LinearLayout.<init>(LinearLayout.java:172) 
      ... 26 more 

    00k的图片直接在xml里设置的话在个别手机很容易内存溢出.试试这个吧/

    public static Bitmap loadBitmapImage(int resId,int inSize,Context context) {
         //resetPurgeTimer();
         String strId =String.valueOf(resId);
          if(mImageCache.containsKey(strId)) {
           SoftReference<Bitmap> softReference = mImageCache.get(strId);
                 Bitmap bitmap = softReference.get();             
                 if(null != bitmap)
                     return bitmap;

               }
               BitmapFactory.Options options = new BitmapFactory.Options();
          options.inSampleSize = inSize;
          options.inPreferredConfig = Bitmap.Config.ARGB_4444;
      Bitmap bitmap =null;
      try{
       bitmap = BitmapFactory.decodeResource(context.getResources(), resId);           
             mImageCache.put(strId, new SoftReference<Bitmap>(bitmap));
      }catch(OutOfMemoryError e){
       System.gc();
       bitmap =null;
      }
               return bitmap;

        }

    public static Drawable readDrawable(Context context, int resId) { 
    BitmapFactory.Options opt = new BitmapFactory.Options(); 
    opt.inPreferredConfig = Bitmap.Config.RGB_565; 
    opt.inPurgeable = true; 
    opt.inInputShareable = true; 
    // 获取资源图片 
    InputStream is = context.getResources().openRawResource(resId); 
    return new BitmapDrawable(BitmapFactory.decodeStream(is, null, opt)); 


    网上说decodeStream内存占用少一些,把你的decodeResouse换成decodeStream 会不会更好一些 
     

    可能性1:

    程序其他地方加载了太多bitmap导致这个地方内存不够了,我觉得这个可能性比较大

    把你这个Activity单独抠出来放到一个独立工程里跑一遍,就跑界面,排除其他逻辑的影响,看看还会不会OOM

    可能性2:

    如果还会OOM,我怀疑你这个layout某些图片用了比较大的分辨率(200-300应该不至于),而那台手机分辨率是比xhdpi还大一个挡位,所以系统自动进行了放大才加载。

    By default, Android scales your bitmap drawables (.png,.jpg, and.giffiles) and Nine-Patch drawables (.9.pngfiles) so that they render at the appropriate physical size on each device. For example, if your application provides bitmap drawables only for the baseline, medium screen density (mdpi), then the system scales them up when on a high-density screen, and scales them down when on a low-density screen.

     
    --- 共有 2 条评论 ---
    • 四档路飞谢谢,原来在xxhdpi下再放个图片文件,系统就不会吧hdpi下的图片拿来缩放从而导致OOM了 (10个月前)  
  • 相关阅读:
    web页面前图标
    leetcode收获
    Shell统计函数耗时(实现数字运算)
    Shell判断数值是否存在于列表
    设置Ubuntu虚拟机硬件时间与系统同步
    Python捕获键盘中断^C方法(Ctrl-C)
    Shell创建zip文件不包含完整路径方法
    jquery判断复选框checkbox是否被选中
    php Base64编码/解码
    php二维数组排序
  • 原文地址:https://www.cnblogs.com/awkflf11/p/5366222.html
Copyright © 2020-2023  润新知