使用 EditTextPreference 的子类时出现 InstantiationException

发布于 2024-11-11 12:42:57 字数 4547 浏览 4 评论 0原文

假设,我有一个名为 PasswordProtectedEditTextPreference 的 EditTextPreference 子类。该子类基本上显示一个密码对话框,然后才能通过 EditTextPreference 自己的对话框编辑首选项。

现在,我在相应的preferences.xml 中定义首选项,如下所示:

<edu.myproject.pwprefs.PasswordProtectedEditTextPreference android:key="pref_password"
            android:title="@string/pref_password_title" android:summary="@string/pref_password_summary"
            android:dialogTitle="@string/pref_password_dialog_title" android:dialogMessage="@string/pref_password_dialog_message">
</edu.myproject.pwprefs.PasswordProtectedEditTextPreference>

然后,通过在onCreate() 期间调用,将preferences.xml 应用到PreferenceActivity 的子类中

addPreferencesFromResource(R.xml.preferences);

。使用 Android 1.5 一切正常。但是,当我使用更高的 Android 版本时,我得到以下堆栈跟踪:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.myproject/edu.myproject.Preferences}: android.view.InflateException: Binary XML file line #25: Error inflating class java.lang.reflect.Constructor
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:144)
     at android.app.ActivityThread.main(ActivityThread.java:4937)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class java.lang.reflect.Constructor
     at android.preference.GenericInflater.createItem(GenericInflater.java:397)
     at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:430)
     at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
     at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
     at android.preference.GenericInflater.inflate(GenericInflater.java:326)
     at android.preference.GenericInflater.inflate(GenericInflater.java:263)
     at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:254)
     at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:268)
     at edu.myproject.Preferences.onCreate(Preferences.java:50)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
     ... 11 more
 Caused by: java.lang.InstantiationException: edu.myproject.pwprefs.PasswordProtectedEditTextPreference
     at java.lang.reflect.Constructor.constructNative(Native Method)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
     at android.preference.GenericInflater.createItem(GenericInflater.java:383)
     ... 21 more

知道这里发生了什么吗?

编辑:堆栈跟踪中的#25对应于preferences.xml中的PasswordProtectedEditTextPreference

编辑:这是我的PasswordProtectedEditTextPreference:

package edu.myproject.pwprefs;

import edu.myproject.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.LinearLayout;

public abstract class PasswordProtectedEditTextPreference extends EditTextPreference {

    public PasswordProtectedEditTextPreference(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public PasswordProtectedEditTextPreference(Context context,
            AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public PasswordProtectedEditTextPreference(Context context,
            AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }


    // further non-instantiation related code
}

Assume, I have a subclass of EditTextPreference called PasswordProtectedEditTextPreference. This subclass basically shows a password dialog before one can edit the preference by the EditTextPreference's own dialog.

Now I define the preference in the corresponding preferences.xml like this:

<edu.myproject.pwprefs.PasswordProtectedEditTextPreference android:key="pref_password"
            android:title="@string/pref_password_title" android:summary="@string/pref_password_summary"
            android:dialogTitle="@string/pref_password_dialog_title" android:dialogMessage="@string/pref_password_dialog_message">
</edu.myproject.pwprefs.PasswordProtectedEditTextPreference>

Then I apply preferences.xml in my subclass of PreferenceActivity by calling

addPreferencesFromResource(R.xml.preferences);

during onCreate(). This all works fine using Android 1.5. However, when I use a higher Android version, I get the following stacktrace:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.myproject/edu.myproject.Preferences}: android.view.InflateException: Binary XML file line #25: Error inflating class java.lang.reflect.Constructor
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:144)
     at android.app.ActivityThread.main(ActivityThread.java:4937)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class java.lang.reflect.Constructor
     at android.preference.GenericInflater.createItem(GenericInflater.java:397)
     at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:430)
     at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
     at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
     at android.preference.GenericInflater.inflate(GenericInflater.java:326)
     at android.preference.GenericInflater.inflate(GenericInflater.java:263)
     at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:254)
     at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:268)
     at edu.myproject.Preferences.onCreate(Preferences.java:50)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
     ... 11 more
 Caused by: java.lang.InstantiationException: edu.myproject.pwprefs.PasswordProtectedEditTextPreference
     at java.lang.reflect.Constructor.constructNative(Native Method)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
     at android.preference.GenericInflater.createItem(GenericInflater.java:383)
     ... 21 more

Any idea what's happening here?

Edit: #25 from the stacktrace corresponds to the PasswordProtectedEditTextPreference in preferences.xml

Edit: This is my PasswordProtectedEditTextPreference:

package edu.myproject.pwprefs;

import edu.myproject.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.LinearLayout;

public abstract class PasswordProtectedEditTextPreference extends EditTextPreference {

    public PasswordProtectedEditTextPreference(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public PasswordProtectedEditTextPreference(Context context,
            AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public PasswordProtectedEditTextPreference(Context context,
            AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }


    // further non-instantiation related code
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹微笑 2024-11-18 12:42:57

您创建了一个抽象类:

  public abstract class PasswordProtectedEditTextPreference  

难怪它无法实例化;-)

You created an abstract class:

  public abstract class PasswordProtectedEditTextPreference  

No wonder it can't be instantiated ;-)

流殇 2024-11-18 12:42:57

通常,这意味着您没有合适的构造函数。确保您的构造函数是公共的,并采用 ContextAttributeSet 作为参数:(

public TimePreference(Context ctxt, AttributeSet attrs)

来自 此自定义首选项来自 此示例项目)

Usually, that means that you do not have an appropriate constructor. Make sure that your constructor is public and takes a Context and AttributeSet as parameters:

public TimePreference(Context ctxt, AttributeSet attrs)

(from this custom preference from this sample project)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文