2014-03-12 80 views
1

我有這個代碼,但我有問題。該應用程序不幸關閉,我不知道這個代碼中的問題是什麼。android ImageView問題

我的XML文件包含幾個ImageViews,但我不知道它爲什麼關閉......是不是有可能有很多ImageViews?

<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) 
+1

'引起:java.lang.OutOfMemoryError' – Blackbelt

+0

什麼是您的101行上的xml文件 –

+0

可能是您的圖像是高分辨率的圖像。 – Piyush

回答

3

這是一個outOfMemory問題正在發生,因爲您的加載在你的應用程序超過了OS分配給您的應用程序存儲太多的圖像。

如果您正在使用高性能設備或者縮放圖像,則可能不會出現此問題。

嘗試使用Adapter來繪製圖像並將它們動態地放入UI。 Adapter僅將當前正在查看的圖像繪製到屏幕中。但在你的代碼中,你試圖在開始時加載所有的圖像。

這裏是一個example如何使用ListView帶適配器來顯示圖像

0

我試着用低質量的圖像你的代碼,它做工精細。您應該使用自定義Adapter類來動態加載圖像。這example將幫助你做到這一點。

+0

我不知道如何使用適配器類。哈哈我還沒有嘗試過,但 – kathleen55

+0

然後,你應該先通過這個[鏈接](http://developer.android.com/guide/topics/ui/layout/gridview.html) – Akshay