无理由地膨胀类时出错

时间:2014-10-30 12:16:05

标签: android xml eclipse imagebutton

我有一个xml文件,假设显示15个图像按钮,但是直到xml文件中的图像按钮数量为5或更低才能正常工作,但是对于更多数量的图像按钮,会出现此错误: java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.ararat / com.example.ararat.Setwallpaper}:android.view.InflateException:二进制XML文件行#35:错误导致类

我的xml文件:

    <ScrollView 
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
  android:fillViewport="true" 
  xmlns:android="http://schemas.android.com/apk/res/android">
  <LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"

    >
     <ImageButton android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/wall_img_1"
         android:src="@drawable/wall1"
         android:layout_marginBottom="5dp"/>
     <ImageButton android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/wall_img_2"
         android:src="@drawable/wall2"
          android:layout_marginBottom="5dp"/>
      <ImageButton android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/wall_img_3"
         android:src="@drawable/wall3"
          android:layout_marginBottom="5dp"/>
        <ImageButton android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/wall_img_4"
         android:layout_marginBottom="5dp"
         android:src="@drawable/wall4"/>
         <ImageButton android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/wall_img_5"
         android:src="@drawable/wall5"
          android:layout_marginBottom="5dp"/>

   </LinearLayout>
  </ScrollView> 

注意:但是当我们添加更多(超过5个)具有相同图像的图像按钮(来自前5个图像按钮的图像,如“@ drawable / wall5”)工作正常。 我试过相对布局和其他任何东西,但这个错误没有,我真的很困惑。
错误日志:

10-31 03:02:38.741: E/AndroidRuntime(1957): FATAL EXCEPTION: main
10-31 03:02:38.741: E/AndroidRuntime(1957): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ararat/com.example.ararat.Setwallpaper}: android.view.InflateException: Binary XML file line #36: Error inflating class <unknown>
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.os.Looper.loop(Looper.java:137)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.app.ActivityThread.main(ActivityThread.java:4340)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at java.lang.reflect.Method.invokeNative(Native Method)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at java.lang.reflect.Method.invoke(Method.java:511)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at dalvik.system.NativeStart.main(Native Method)
10-31 03:02:38.741: E/AndroidRuntime(1957): Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class <unknown>
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.view.LayoutInflater.createView(LayoutInflater.java:606)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.app.Activity.setContentView(Activity.java:1835)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at com.example.ararat.Setwallpaper.onCreate(Setwallpaper.java:23)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.app.Activity.performCreate(Activity.java:4465)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-31 03:02:38.741: E/AndroidRuntime(1957):     ... 11 more
10-31 03:02:38.741: E/AndroidRuntime(1957): Caused by: java.lang.reflect.InvocationTargetException
10-31 03:02:38.741: E/AndroidRuntime(1957):     at java.lang.reflect.Constructor.constructNative(Native Method)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.view.LayoutInflater.createView(LayoutInflater.java:586)
10-31 03:02:38.741: E/AndroidRuntime(1957):     ... 25 more
10-31 03:02:38.741: E/AndroidRuntime(1957): Caused by: java.lang.OutOfMemoryError
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.content.res.Resources.loadDrawable(Resources.java:1937)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.widget.ImageView.<init>(ImageView.java:119)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.widget.ImageButton.<init>(ImageButton.java:85)
10-31 03:02:38.741: E/AndroidRuntime(1957):     at android.widget.ImageButton.<init>(ImageButton.java:81)
10-31 03:02:38.741: E/AndroidRuntime(1957):     ... 28 more

2 个答案:

答案 0 :(得分:0)

膨胀异常实际上并不是问题,但实际上来自布局中的另一个更深层次的问题,然后将其包装在InflateException中。一个常见问题是在尝试加载加载可绘制资源的imageview时出现内存不足异常。如果其中一个资源具有较高的像素分辨率,则需要大量内存才会导致膨胀异常。

因此,基本上验证drawables图像中的像素分辨率只是布局所需的最小值。

答案 1 :(得分:0)

由于许多图片,您的设备内存不足,请尝试使用

   android:largeHeap="true"

在Menifest文件的标签中

     <application 
 android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"  >