11-11 09:10:09.395: ERROR/AndroidRuntime(523): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amaker.wlo/com.amaker.wlo.OrderDishesActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.os.Looper.loop(Looper.java:123)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at java.lang.reflect.Method.invoke(Method.java:521)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at dalvik.system.NativeStart.main(Native Method)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.app.Activity.setContentView(Activity.java:1622)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at com.amaker.wlo.OrderDishesActivity.onCreate(OrderDishesActivity.java:60)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): ... 11 more
11-11 09:10:09.395: ERROR/AndroidRuntime(523): Caused by: java.lang.reflect.InvocationTargetException
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.widget.RelativeLayout.<init>(RelativeLayout.java:171)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at java.lang.reflect.Constructor.constructNative(Native Method)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): ... 21 more
11-11 09:10:09.395: ERROR/AndroidRuntime(523): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:447)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.content.res.Resources.loadDrawable(Resources.java:1705)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.view.View.<init>(View.java:1850)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.view.View.<init>(View.java:1799)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): at android.view.ViewGroup.<init>(ViewGroup.java:284)
11-11 09:10:09.395: ERROR/AndroidRuntime(523): ... 25 more
8 个解决方案
#1
看log,
重点在下面两行:
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
内存溢出了,XML 文件有错,检查下吧
重点在下面两行:
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
内存溢出了,XML 文件有错,检查下吧
#2
大图片解析造成的OOM
用这种方式解析
用这种方式解析
01./**
02. * 加载大图片工具类:解决android加载大图片时报OOM异常
03. * 解决原理:先设置缩放选项,再读取缩放的图片数据到内存,规避了内存引起的OOM
07. */
08.public class BitmapUtil {
09.
10. public static final int UNCONSTRAINED = -1;
11.
12. /*
13. * 获得设置信息
14. */
15. public static Options getOptions(String path){
16. Options options = new Options();
17. options.inJustDecodeBounds = true;//只描边,不读取数据
18. BitmapFactory.decodeFile(path, options);
19. return options;
20. }
21.
22.
23. /**
24. * 获得图像
25. * @param path
26. * @param options
27. * @return
28. * @throws FileNotFoundException
29. */
30. public static Bitmap getBitmapByPath(String path, Options options , int screenWidth , int screenHeight)throws FileNotFoundException{
31. File file = new File(path);
32. if(!file.exists()){
33. throw new FileNotFoundException();
34. }
35. FileInputStream in = null;
36. in = new FileInputStream(file);
37. if(options != null){
38. Rect r = getScreenRegion(screenWidth,screenHeight);
39. int w = r.width();
40. int h = r.height();
41. int maxSize = w > h ? w : h;
42. int inSimpleSize = computeSampleSize(options, maxSize, w * h);
43. options.inSampleSize = inSimpleSize; //设置缩放比例
44. options.inJustDecodeBounds = false;
45. }
46. Bitmap b = BitmapFactory.decodeStream(in, null, options);
47. try {
48. in.close();
49. } catch (IOException e) {
50. e.printStackTrace();
51. }
52. return b;
53. }
54.
55.
56.
57. private static Rect getScreenRegion(int width , int height) {
58. return new Rect(0,0,width,height);
59. }
60.
61.
62. /**
63. * 获取需要进行缩放的比例,即options.inSampleSize
64. * @param options
65. * @param minSideLength
66. * @param maxNumOfPixels
67. * @return
68. */
69. public static int computeSampleSize(BitmapFactory.Options options,
70. int minSideLength, int maxNumOfPixels) {
71. int initialSize = computeInitialSampleSize(options, minSideLength,
72. maxNumOfPixels);
73.
74. int roundedSize;
75. if (initialSize <= 8) {
76. roundedSize = 1;
77. while (roundedSize < initialSize) {
78. roundedSize <<= 1;
79. }
80. } else {
81. roundedSize = (initialSize + 7) / 8 * 8;
82. }
83.
84. return roundedSize;
85. }
86.
87. private static int computeInitialSampleSize(BitmapFactory.Options options,
88. int minSideLength, int maxNumOfPixels) {
89. double w = options.outWidth;
90. double h = options.outHeight;
91.
92. int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 :
93. (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
94. int upperBound = (minSideLength == UNCONSTRAINED) ? 128 :
95. (int) Math.min(Math.floor(w / minSideLength),
96. Math.floor(h / minSideLength));
97.
98. if (upperBound < lowerBound) {
99. // return the larger one when there is no overlapping zone.
100. return lowerBound;
101. }
102.
103. if ((maxNumOfPixels == UNCONSTRAINED) &&
104. (minSideLength == UNCONSTRAINED)) {
105. return 1;
106. } else if (minSideLength == UNCONSTRAINED) {
107. return lowerBound;
108. } else {
109. return upperBound;
110. }
111. }
112.
113.
114.}
#3
可以这样调用
String path = "/sdcard/test2.jpg";
try {
Bitmap bitmap = BitmapUtil.getBitmapByPath(path, BitmapUtil.getOptions(path), screenWidth, screenHeight);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
#4
确定是大图片引起的?
#5
基本可以确定,因为这问题我在做某个公司外包相册的时候也碰到过。
#6
也可以检查一下是否存在 bitmap 没有及时释放造成的内存问题;因为在第一次装载的时候没有问题,在 home 按下之后才出现这个问题,在 onPause 的时候释放一下内存吧
#7
+1
Bitmap不用了以后最好是调用recycle()方法释放掉
#8
//创建Bitmap 缩略图
public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);
int roundedSize;
if (initialSize <= 8 ) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
//创建Bitmap 缩略图
private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength),Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
//调用
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile("/sdcard/imageview/"+ct.getPiv(), opts);
opts.inSampleSize = computeSampleSize(opts, -1, 128*128);
opts.inJustDecodeBounds = false;
try {
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/imageview/"+ct.getPiv(), opts);
imageView.setImageBitmap(bitmap);
} catch (OutOfMemoryError err) {
}
#1
看log,
重点在下面两行:
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
内存溢出了,XML 文件有错,检查下吧
重点在下面两行:
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
内存溢出了,XML 文件有错,检查下吧
#2
大图片解析造成的OOM
用这种方式解析
用这种方式解析
01./**
02. * 加载大图片工具类:解决android加载大图片时报OOM异常
03. * 解决原理:先设置缩放选项,再读取缩放的图片数据到内存,规避了内存引起的OOM
07. */
08.public class BitmapUtil {
09.
10. public static final int UNCONSTRAINED = -1;
11.
12. /*
13. * 获得设置信息
14. */
15. public static Options getOptions(String path){
16. Options options = new Options();
17. options.inJustDecodeBounds = true;//只描边,不读取数据
18. BitmapFactory.decodeFile(path, options);
19. return options;
20. }
21.
22.
23. /**
24. * 获得图像
25. * @param path
26. * @param options
27. * @return
28. * @throws FileNotFoundException
29. */
30. public static Bitmap getBitmapByPath(String path, Options options , int screenWidth , int screenHeight)throws FileNotFoundException{
31. File file = new File(path);
32. if(!file.exists()){
33. throw new FileNotFoundException();
34. }
35. FileInputStream in = null;
36. in = new FileInputStream(file);
37. if(options != null){
38. Rect r = getScreenRegion(screenWidth,screenHeight);
39. int w = r.width();
40. int h = r.height();
41. int maxSize = w > h ? w : h;
42. int inSimpleSize = computeSampleSize(options, maxSize, w * h);
43. options.inSampleSize = inSimpleSize; //设置缩放比例
44. options.inJustDecodeBounds = false;
45. }
46. Bitmap b = BitmapFactory.decodeStream(in, null, options);
47. try {
48. in.close();
49. } catch (IOException e) {
50. e.printStackTrace();
51. }
52. return b;
53. }
54.
55.
56.
57. private static Rect getScreenRegion(int width , int height) {
58. return new Rect(0,0,width,height);
59. }
60.
61.
62. /**
63. * 获取需要进行缩放的比例,即options.inSampleSize
64. * @param options
65. * @param minSideLength
66. * @param maxNumOfPixels
67. * @return
68. */
69. public static int computeSampleSize(BitmapFactory.Options options,
70. int minSideLength, int maxNumOfPixels) {
71. int initialSize = computeInitialSampleSize(options, minSideLength,
72. maxNumOfPixels);
73.
74. int roundedSize;
75. if (initialSize <= 8) {
76. roundedSize = 1;
77. while (roundedSize < initialSize) {
78. roundedSize <<= 1;
79. }
80. } else {
81. roundedSize = (initialSize + 7) / 8 * 8;
82. }
83.
84. return roundedSize;
85. }
86.
87. private static int computeInitialSampleSize(BitmapFactory.Options options,
88. int minSideLength, int maxNumOfPixels) {
89. double w = options.outWidth;
90. double h = options.outHeight;
91.
92. int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 :
93. (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
94. int upperBound = (minSideLength == UNCONSTRAINED) ? 128 :
95. (int) Math.min(Math.floor(w / minSideLength),
96. Math.floor(h / minSideLength));
97.
98. if (upperBound < lowerBound) {
99. // return the larger one when there is no overlapping zone.
100. return lowerBound;
101. }
102.
103. if ((maxNumOfPixels == UNCONSTRAINED) &&
104. (minSideLength == UNCONSTRAINED)) {
105. return 1;
106. } else if (minSideLength == UNCONSTRAINED) {
107. return lowerBound;
108. } else {
109. return upperBound;
110. }
111. }
112.
113.
114.}
#3
可以这样调用
String path = "/sdcard/test2.jpg";
try {
Bitmap bitmap = BitmapUtil.getBitmapByPath(path, BitmapUtil.getOptions(path), screenWidth, screenHeight);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
#4
确定是大图片引起的?
#5
基本可以确定,因为这问题我在做某个公司外包相册的时候也碰到过。
#6
也可以检查一下是否存在 bitmap 没有及时释放造成的内存问题;因为在第一次装载的时候没有问题,在 home 按下之后才出现这个问题,在 onPause 的时候释放一下内存吧
#7
+1
Bitmap不用了以后最好是调用recycle()方法释放掉
#8
//创建Bitmap 缩略图
public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);
int roundedSize;
if (initialSize <= 8 ) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
//创建Bitmap 缩略图
private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength),Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
//调用
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile("/sdcard/imageview/"+ct.getPiv(), opts);
opts.inSampleSize = computeSampleSize(opts, -1, 128*128);
opts.inJustDecodeBounds = false;
try {
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/imageview/"+ct.getPiv(), opts);
imageView.setImageBitmap(bitmap);
} catch (OutOfMemoryError err) {
}