【问题标题】:Inflater can't find constructor充气机找不到构造函数
【发布时间】:2011-02-02 18:17:06
【问题描述】:

这可能是我遇到过的最烦人的错误。当我去膨胀自定义视图并将其传递给活动时,我得到了这个:

02-02 10:34:08.080: ERROR/AndroidRuntime(839): java.lang.RuntimeException: Unable to start activity 

ComponentInfo{android.appion.ResourceManager.UI/android.appion.ResourceManager.UI.ARMHomescreen}: android.view.InflateException: Binary XML file line #62: Error inflating class android.appion.ResourceManager.NUI.ARMPublicService$WorkBench
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.os.Looper.loop(Looper.java:144)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.app.ActivityThread.main(ActivityThread.java:4937)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at java.lang.reflect.Method.invokeNative(Native Method)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at java.lang.reflect.Method.invoke(Method.java:521)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at dalvik.system.NativeStart.main(Native Method)
02-02 10:34:08.080: ERROR/AndroidRuntime(839): Caused by: android.view.InflateException: Binary XML file line #62: Error inflating class android.appion.ResourceManager.NUI.ARMPublicService$WorkBench
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.view.LayoutInflater.createView(LayoutInflater.java:503)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.app.Activity.setContentView(Activity.java:1654)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.appion.ResourceManager.UI.ARMHomescreen.onCreate(ARMHomescreen.java:141)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     ... 11 more
02-02 10:34:08.080: ERROR/AndroidRuntime(839): Caused by: java.lang.NoSuchMethodException: WorkBench(Context,AttributeSet)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at java.lang.Class.getMatchingConstructor(Class.java:660)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at java.lang.Class.getConstructor(Class.java:477)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     at android.view.LayoutInflater.createView(LayoutInflater.java:475)
02-02 10:34:08.080: ERROR/AndroidRuntime(839):     ... 23 more

我从中得到的是“嘿,我一定忘记将 AttributeSet 添加到我的视图中,笨蛋”,事实并非如此!据我所知,我的构造函数是完美的。谁能告诉我我错过了什么,或者日食是否在直肠上搞砸了我?

public class WorkBench extends GridView {
    private final String TAG = "WorkBench";

    // The position in the WorkArea
    public String TITLE;
    private String[] DEVICE = new String[6];

    private int mSelection;
    public WorkBench(Context context) {super(context);}
    public WorkBench(Context context, AttributeSet attrs) {
        super(context, attrs);
        setAdapter(new GridAdapter());
    }
    public WorkBench(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

... Rest of class definition

我在哪里调用 WorkBench(WorkBench 和这个函数在一个服务中,这就是我转换为 Activity 的原因)

public WorkBench createBench(Context context) {
    return (WorkBench)((Activity)context).findViewById(R.id.workbench);
}

还有 XML

<view               class="android.appion.ResourceManager.NUI.ARMPublicService$WorkBench"
    android:id="@+id/workbench"
    android:background="@+drawable/woodtexture1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="2" />

任何提示都会有所帮助,谢谢 ~伊顿

【问题讨论】:

    标签: android view inflate


    【解决方案1】:

    要么你的class 错了,要么你的班级错了。

    你有:

    class="android.appion.ResourceManager.NUI.ARMPublicService$WorkBench"
    

    当且仅当WorkBench 是静态内部类时,这才有效。如上所示,WorkBench 的类定义是针对非静态内部类的。

    由于在这种情况下不可能使用非静态内部类,您的 class 可能没问题 - 只需将 WorkBench 调整为静态内部类,您的状态应该没问题。

    【讨论】:

    • 啊,我明白了……虽然我不能使内部类静态,它需要经常访问外部类的数据。正如你上面所说,在这种情况下不可能使用非静态类,有没有办法改变这种情况以适应非静态内部类?还是所有自定义视图都必须是静态周期?
    • @AedonEtLIRA:“我不能让内部类成为静态的,它需要经常访问外部类的数据。”——你别无选择。 “或者所有自定义视图都必须是静态周期?” -- 所有自定义视图必须是独立类或静态内部类。从外部实例化非静态内部类在物理上是不可能的。
    猜你喜欢
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2014-04-23
    • 2013-01-16
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多