4

我添加了一个 ImageView 并分配了一个 jpg 文件(a1.jpg),该文件为 770Kb。当我运行项目时,它会崩溃并显示很多错误日志。我尝试了另一个 jpg 文件,大小为 70KB 并运行该项目,这没有问题。为什么?谢谢!

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/Test_string"
    android:src="@drawable/a1" />

错误日志

02-09 12:47:34.692: D/AndroidRuntime(1023): Shutting down VM
02-09 12:47:34.692: W/dalvikvm(1023): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-09 12:47:34.802: E/AndroidRuntime(1023): FATAL EXCEPTION: main
02-09 12:47:34.802: E/AndroidRuntime(1023): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lighter.androidntest4/com.lighter.androidntest4.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.os.Looper.loop(Looper.java:137)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at java.lang.reflect.Method.invokeNative(Native Method)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at java.lang.reflect.Method.invoke(Method.java:511)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at dalvik.system.NativeStart.main(Native Method)
02-09 12:47:34.802: E/AndroidRuntime(1023): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.app.Activity.setContentView(Activity.java:1881)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at com.lighter.androidntest4.MainActivity.onCreate(MainActivity.java:15)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.app.Activity.performCreate(Activity.java:5104)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-09 12:47:34.802: E/AndroidRuntime(1023):     ... 11 more
02-09 12:47:34.802: E/AndroidRuntime(1023): Caused by: java.lang.reflect.InvocationTargetException
02-09 12:47:34.802: E/AndroidRuntime(1023):     at java.lang.reflect.Constructor.constructNative(Native Method)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
02-09 12:47:34.802: E/AndroidRuntime(1023):     ... 24 more
02-09 12:47:34.802: E/AndroidRuntime(1023): Caused by: java.lang.OutOfMemoryError
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.content.res.Resources.loadDrawable(Resources.java:1965)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.widget.ImageView.<init>(ImageView.java:120)
02-09 12:47:34.802: E/AndroidRuntime(1023):     at android.widget.ImageView.<init>(ImageView.java:110)
02-09 12:47:34.802: E/AndroidRuntime(1023):     ... 27 more
4

4 回答 4

6

这可能是因为您试图加载太大的文件。即使你的错误是这样说的:

Caused by: java.lang.OutOfMemoryError

770KB 的图像对于 android 应用程序来说是相当大的。你真的应该考虑一些更小的事情。

您还可以在 AVD 中增加模拟器的堆大小,但在您的情况下,如果可以的话,最好使用较小的图像。

于 2013-02-09T14:10:58.300 回答
0

嗯...听起来您的可绘制对象可能设置不正确。你确定名字等吗?

于 2013-02-09T14:09:21.173 回答
0

模拟器无法支持那么大的图像,如 所示java.lang.OutOfMemoryError。您应该能够通过进入 AVD 管理器来自定义模拟器以允许更大的内存(我相信它是 Max VM 应用程序堆大小)。

于 2013-02-09T14:13:40.840 回答
0

我有同样的问题,Piotr Chojnacki 是对的;这是一个图像大小问题。我认为这对于旧设备和模拟器来说是一个更大的问题。

于 2013-07-03T18:43:48.730 回答