android ImageView问题

时间:2014-03-12 11:58:44

标签: android

我有这个代码,但我遇到了问题。该应用程序很遗憾地关闭了,我不知道此代码中的问题是什么。

我的XML文件包含多个ImageView,但我不知道它为什么关闭...是不是有可能有很多ImageView?

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/a" />

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/b"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/c"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/d"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/e"/>

     <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/f"/>

      <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/g"/>     

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/h"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/i"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/j"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/k"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/l"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/m"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/n"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/o"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/p"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/q"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/r"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/s"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/t"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/u"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/v"/>

     <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/w"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/x"/>

    <ImageView 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/y"/>


</LinearLayout>


 </ScrollView>

这里是LogCat:

03-12 11:37:55.481: E/AndroidRuntime(879): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesis.americansignlanguage/com.thesis.americansignlanguage.AlphabetTutorial}: android.view.InflateException: Binary XML file line #101: Error inflating class <unknown>
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.os.Looper.loop(Looper.java:137)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-12 11:37:55.481: E/AndroidRuntime(879):  at java.lang.reflect.Method.invokeNative(Native Method)
03-12 11:37:55.481: E/AndroidRuntime(879):  at java.lang.reflect.Method.invoke(Method.java:511)
03-12 11:37:55.481: E/AndroidRuntime(879):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-12 11:37:55.481: E/AndroidRuntime(879):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-12 11:37:55.481: E/AndroidRuntime(879):  at dalvik.system.NativeStart.main(Native Method)
03-12 11:37:55.481: E/AndroidRuntime(879): Caused by: android.view.InflateException: Binary XML file line #101: Error inflating class <unknown>
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.view.LayoutInflater.createView(LayoutInflater.java:613)
03-12 11:37:55.481: E/AndroidRuntime(879):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-12 11:37:55.481: E/AndroidRuntime(879):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.app.Activity.setContentView(Activity.java:1881)
03-12 11:37:55.481: E/AndroidRuntime(879):  at com.thesis.americansignlanguage.AlphabetTutorial.onCreate(AlphabetTutorial.java:12)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.app.Activity.performCreate(Activity.java:5104)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-12 11:37:55.481: E/AndroidRuntime(879):  ... 11 more
03-12 11:37:55.481: E/AndroidRuntime(879): Caused by: java.lang.reflect.InvocationTargetException
03-12 11:37:55.481: E/AndroidRuntime(879):  at java.lang.reflect.Constructor.constructNative(Native Method)
03-12 11:37:55.481: E/AndroidRuntime(879):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.view.LayoutInflater.createView(LayoutInflater.java:587)
03-12 11:37:55.481: E/AndroidRuntime(879):  ... 25 more
03-12 11:37:55.481: E/AndroidRuntime(879): Caused by: java.lang.OutOfMemoryError
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.content.res.Resources.loadDrawable(Resources.java:1965)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.widget.ImageView.<init>(ImageView.java:120)
03-12 11:37:55.481: E/AndroidRuntime(879):  at android.widget.ImageView.<init>(ImageView.java:110)
03-12 11:37:55.481: E/AndroidRuntime(879):  ... 28 more
03-12 11:37:57.390: I/Process(879): Sending signal. PID: 879 SIG: 9
03-12 11:37:58.420: E/Trace(949): error opening trace file: No such file or directory (2)

2 个答案:

答案 0 :(得分:3)

这是一个outOfMemory问题,因为您在应用程序中加载的图像数量超出OS分配给您的应用程序的内存。

如果您使用的是高性能设备或正在缩放图像,则可能不会出现此问题。

尝试使用Adapter绘制图片并动态地将它们放入UI中。 Adapter仅将您正在查看的图像绘制到屏幕中。但是在你的代码中,你试图在开始时加载所有图像。

以下是example如何使用ListView和适配器来显示图像

答案 1 :(得分:0)

我尝试使用低质量图像的代码,它工作正常。您应该使用自定义Adapter类动态加载图像。这个example将帮助您实现目标。