Android:膨胀布局时出现带有 InvocationTargetException 的 ***Error

Posted

技术标签:

【中文标题】Android:膨胀布局时出现带有 InvocationTargetException 的 ***Error【英文标题】:Android: ***Error with InvocationTargetException when inflating layout 【发布时间】:2012-02-27 07:17:48 【问题描述】:

首先,对不起我的英语。膨胀布局时我有一个问题:

我有一个自定义视图,它从一个名为 ButtonHelp 的 LinearLayout 扩展而来。我在名为 LoadingActivity 的布局上使用该视图。我的以下代码在所有设备和模拟器上都能完美运行,除了带有 QVGA 屏幕的设备和模拟器,如三星 Galaxy Y,并出现以下错误:

FATAL EXCEPTION: main  
java.lang.RuntimeException: Unable to start activity ComponentInfocom.myapp/com.myapp.LoadingActivity: android.view.InflateException: Binary XML file line #45: Error inflating class com.myapp.customviews.ButtonHelp  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)  
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)  
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)  
    at android.os.Handler.dispatchMessage(Handler.java:99)  
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #45: Error inflating class com.myapp.customviews.ButtonHelp
    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
    at android.app.Activity.setContentView(Activity.java:1657)
    at com.myapp.LoadingActivity.onCreate(LoadingActivity.java:45)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    ... 11 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
    ... 21 more
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.widget.ImageButton
    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.myapp.customviews.ButtonHelp.initLayout(ButtonHelp.java:29)
    at com.myapp.customviews.ButtonHelp.<init>(ButtonHelp.java:17)
    ... 24 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
    ... 32 more
Caused by: java.lang.***Error
    at android.util.LongSparseArray.get(LongSparseArray.java:79)
    at android.content.res.Resources.getCachedDrawable(Resources.java:1746)
    at android.content.res.Resources.loadDrawable(Resources.java:1664)
    at android.content.res.Resources.getDrawable(Resources.java:581)
    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
    at android.content.res.Resources.loadDrawable(Resources.java:1694)
    at android.content.res.Resources.getDrawable(Resources.java:581)
    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.

问题是,我不知道这条线:

Caused by: java.lang.***Error
    at android.util.LongSparseArray.get(LongSparseArray.java:79)

为什么会发生在 QVGA 模拟器/设备上?

仅供参考:

我只有 320x480 屏幕的图像资源。

ButtonHelp 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >

<ImageButton
    android:id="@+id/btnhelp_help"
    android:layout_
    android:layout_
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/loading_button_help"
    android:src="@drawable/loading_icon_help" />
</LinearLayout>

按钮帮助代码:

public class ButtonHelp extends LinearLayout 
    private ImageButton mHelp;

    public ButtonHelp(Context context, AttributeSet attrs) 
        super(context, attrs);
        initLayout();
        initControls();
    

    public ButtonHelp(Context context) 
        super(context);
        initLayout();
        initControls();
    

    private void initLayout() 
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.btn_help, this);
    

    private void initControls() 
        mHelp = (ImageButton) findViewById(R.id.btnhelp_help);
    

    public void setOnClickListener(View.OnClickListener listener) 
        mHelp.setOnClickListener(listener);
    

ButtonHelp用于LoadingActivity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_
    android:layout_
    android:background="@drawable/loading_background"
    android:orientation="vertical" >

...

    <com.myapp.customviews.ButtonHelp
        android:id="@+id/loading_help"
        android:layout_
        android:layout_
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

...

</RelativeLayout>
当我删除ButtonHelp时,创建布局时仍然出现错误,就像上面的logcat一样。

我已经搜索了另一个 SO 问题,但仍然没有针对此案例的解决方案或解释。

非常感谢!


更新:

根据要求,以下是loading_button_help 的代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/loading_button_help" /> <!-- Normal -->

    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/loading_button_help" /> <!--  Normal disable -->

    <item android:state_pressed="true" 
        android:drawable="@drawable/loading_button_help_press" /> <!-- pressed -->

    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/loading_button_help" /> <!-- selected -->

    <item android:state_enabled="true"
        android:drawable="@drawable/loading_button_help" /> <!-- normal -->

    <item android:state_focused="true"
        android:drawable="@drawable/loading_button_help" /> <!-- normal disable focused -->

    <item
        android:drawable="@drawable/loading_button_help" /> <!-- normal disable -->
</selector>

loading_icon_help 只是一张 PNG 图像。

【问题讨论】:

什么是loading_button_helploading_icon_help 可绘制对象? @a.ch. loading_button_help: 是可绘制的 xml,定义了按钮状态,loading_icon_help 是可绘制的图像。 你能提供他们的xml吗? @a.ch.我已经在问题正文中更新了它们。 【参考方案1】:

在 loading_button_help.xml 你使用@drawable/loading_button_help?我认为你有循环参考。尝试重命名您的 xml 或图像文件。

【讨论】:

谢谢@appserv。我的不好,不知道为什么我第一时间没有注意到。

以上是关于Android:膨胀布局时出现带有 InvocationTargetException 的 ***Error的主要内容,如果未能解决你的问题,请参考以下文章

在 ListView 中设置页脚布局时出现问题

尝试为 Android 扩展 XML 布局时出现 Eclipse ClassCastException

回收视图膨胀不同的行:- 绑定数据时出现异常

在表格布局中使用 android:imeOptions="actionNext" 时出现问题

在 DialogFragment 中为 AlertDialog 膨胀自定义视图时出现问题

膨胀视图以供 AlertDialog 使用时出现“避免将 null 作为视图根传递”警告