当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Fatal exception: main when adding imageview

问题描述:

My app runs fine with just text, but when I add an image I get errors. This is what I get:

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

This is my 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"/>

Anyone have any ideas?

网友答案:

This line:

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

Points to the problem. At a guess, you have an error in you XML at line 17, however, I really think this is the problem:

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

Which means you are trying to add an image which is too big for the device. You will need to resize the image first with something like Bitmap.createScaledBitmap() or BitmapFactory.decodeResource(). There is a lot more info on handling larger images on the Android Loading Large Bitmaps Efficiently page.

分享给朋友:
您可能感兴趣的文章:
随机阅读: