致命异常:添加imageview时主要

时间:2013-12-30 02:07:40

标签: android eclipse

我的应用程序运行正常,只有文本,但当我添加图像时,我得到错误。这就是我得到的:

12-29 20:16:53.105: D/dalvikvm(1515): GC_FOR_MALLOC freed <1K, 51% free 2665K/5379K, external 716K/1038K, paused 2ms
12-29 20:16:53.105: E/GraphicsJNI(1515): VM won't let us allocate 23970816 bytes
12-29 20:16:53.105: D/skia(1515): --- decoder->decode returned false
12-29 20:16:53.105: D/AndroidRuntime(1515): Shutting down VM
12-29 20:16:53.105: W/dalvikvm(1515): threadid=1: thread exiting with uncaught exception (group=0xb6feb4f0)
12-29 20:16:53.105: E/AndroidRuntime(1515): FATAL EXCEPTION: main
12-29 20:16:53.105: E/AndroidRuntime(1515): java.lang.RuntimeException: Unable to start activity ComponentInfo{lanaux.play.awesome/lanaux.play.awesome.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class android.widget.ImageView
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.os.Looper.loop(Looper.java:130)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-29 20:16:53.105: E/AndroidRuntime(1515): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 20:16:53.105: E/AndroidRuntime(1515): at java.lang.reflect.Method.invoke(Method.java:507)
12-29 20:16:53.105: E/AndroidRuntime(1515): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-29 20:16:53.105: E/AndroidRuntime(1515): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-29 20:16:53.105: E/AndroidRuntime(1515): at dalvik.system.NativeStart.main(Native Method)
12-29 20:16:53.105: E/AndroidRuntime(1515): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.widget.ImageView
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
12-29 20:16:53.105: E/AndroidRuntime(1515): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-29 20:16:53.105: E/AndroidRuntime(1515): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.app.Activity.setContentView(Activity.java:1657)
12-29 20:16:53.105: E/AndroidRuntime(1515): at lanaux.play.awesome.MainActivity.onCreate(MainActivity.java:12)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-29 20:16:53.105: E/AndroidRuntime(1515): ... 11 more
12-29 20:16:53.105: E/AndroidRuntime(1515): Caused by: java.lang.reflect.InvocationTargetException
12-29 20:16:53.105: E/AndroidRuntime(1515): at java.lang.reflect.Constructor.constructNative(Native Method)
12-29 20:16:53.105: E/AndroidRuntime(1515): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
12-29 20:16:53.105: E/AndroidRuntime(1515): ... 22 more
12-29 20:16:53.105: E/AndroidRuntime(1515): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.content.res.Resources.loadDrawable(Resources.java:1709)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.widget.ImageView.(ImageView.java:118)
12-29 20:16:53.105: E/AndroidRuntime(1515): at android.widget.ImageView.(ImageView.java:108)
12-29 20:16:53.105: E/AndroidRuntime(1515): ... 25 more

这是我的activity_main.xml:

http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />


    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="68dp"
    android:src="@drawable/christmas2" 
    android:contentDescription="happy holiday"/>

有人有什么想法吗?

1 个答案:

答案 0 :(得分:2)

这一行:

Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.widget.ImageView

指出问题所在。猜测,你在第17行的XML中有一个错误,但是,我真的认为这是问题所在:

Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget

这意味着您尝试添加的图像对于设备来说太大了。您需要先使用Bitmap.createScaledBitmap()BitmapFactory.decodeResource()等内容调整图片大小。有关处理Android Loading Large Bitmaps Efficiently页面上较大图像的更多信息。