android开发,程序运行没有问题,但是按Home键在回到本程序就会报错
错误代码:Logcat
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
[解决办法]
//创建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) { }