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

时间:2021-08-04 16:57:54

=========== 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个月前)