带有JPEG源的ImageButton会导致InflateException

时间:2014-10-18 09:10:38

标签: java android xml android-layout

我有15个imageButtons,其src属性设置为JPEG图片:

    <ImageButton
        android:id="@+id/button01"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.2"
        android:padding="5dp"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        android:background="@drawable/button_state"
        android:src="@drawable/agri_b" />

当我运行我的应用程序时,出现InflateException错误。我尝试将Activity课改为FragmentActivity课,但没有改变。我想我的JPEG图片大小有问题。然而,我最大的图片有217 KB的大小。 这是我得到的错误:

10-18 12:27:32.538: E/AndroidRuntime(30713): FATAL EXCEPTION: main
10-18 12:27:32.538: E/AndroidRuntime(30713): Process: com.example.TraceYourExpense, PID: 30713
10-18 12:27:32.538: E/AndroidRuntime(30713): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.TraceYourExpense/com.example.TraceYourExpense.NewBank}: android.view.InflateException: Binary XML file line #263: Error inflating class <unknown>
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.app.ActivityThread.access$900(ActivityThread.java:173)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.os.Looper.loop(Looper.java:136)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.app.ActivityThread.main(ActivityThread.java:5579)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at java.lang.reflect.Method.invokeNative(Native Method)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at java.lang.reflect.Method.invoke(Method.java:515)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at dalvik.system.NativeStart.main(Native Method)
10-18 12:27:32.538: E/AndroidRuntime(30713): Caused by: android.view.InflateException: Binary XML file line #263: Error inflating class <unknown>
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.app.Activity.setContentView(Activity.java:2031)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at com.example.TraceYourExpense.NewBank.onCreate(NewBank.java:51)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.app.Activity.performCreate(Activity.java:5451)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
10-18 12:27:32.538: E/AndroidRuntime(30713):    ... 11 more
10-18 12:27:32.538: E/AndroidRuntime(30713): Caused by: java.lang.reflect.InvocationTargetException
10-18 12:27:32.538: E/AndroidRuntime(30713):    at java.lang.reflect.Constructor.constructNative(Native Method)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
10-18 12:27:32.538: E/AndroidRuntime(30713):    ... 26 more
10-18 12:27:32.538: E/AndroidRuntime(30713): Caused by: java.lang.OutOfMemoryError
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.content.res.Resources.loadDrawable(Resources.java:3454)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.widget.ImageView.<init>(ImageView.java:133)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.widget.ImageButton.<init>(ImageButton.java:88)
10-18 12:27:32.538: E/AndroidRuntime(30713):    at android.widget.ImageButton.<init>(ImageButton.java:84)
10-18 12:27:32.538: E/AndroidRuntime(30713):    ... 29 more

任何帮助都将不胜感激。

修改-1

要检查错误是否与图片大小有关,我从ImageButtons删除了所有图片来源,但我意外地再次收到错误!!严重的问题在哪里?

2 个答案:

答案 0 :(得分:0)

尝试

android:layout_width="0dp"

(如果您的图像在其父视图中水平显示) 或

 android:layout_height="0dp" 

(如果它们在其父级内垂直显示)。如果您正在使用layout_weight,则图片的宽度或高度应始终为0dp。

答案 1 :(得分:0)

可能是您的代码存在问题

<ImageButton
    android:id="@+id/button01"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.2"
    android:padding="5dp"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:background="@android:color/transparent"
    android:src="@drawable/ic_launcher" />

尝试使用此代码..如果这有效..那么问题可能就是..你在背景和src上都设置了drawable。为什么你会收到错误..