Android 使用EditTextPreference子类时的实例化异常
假设我有一个名为PasswordProtectedEditTextPreference的子类Android 使用EditTextPreference子类时的实例化异常,android,exception,instantiation,inflate,Android,Exception,Instantiation,Inflate,假设我有一个名为PasswordProtectedEditTextPreference的子类EditTextPreference。这个子类基本上显示了一个密码对话框,然后可以通过EditTextPreference自己的对话框编辑首选项 现在,我在相应的preferences.xml中定义首选项,如下所示: <edu.myproject.pwprefs.PasswordProtectedEditTextPreference android:key="pref_password"
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()
期间。使用安卓1.5,这一切都很好。但是,当我使用更高版本的Android时,我会得到以下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
知道这里发生了什么吗
编辑:#stacktrace中的25对应于preferences.xml中的PasswordProtectedEditTextPreference
编辑:这是我的密码ProtectedEditTextPreference:
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
}
通常,这意味着您没有合适的构造函数。确保构造函数是公共的,并将
上下文
和属性集
作为参数:
public TimePreference(Context ctxt, AttributeSet attrs)
(from)您创建了一个抽象类:
难怪它不能被实例化;-) 如果这还不能解决问题,请添加您的首选项代码,可能还有其他问题。希望这么简单-构造函数已经就位,我怀疑它在1.5中不会运行。@ubuntudroid:事实上,构造函数的组合已经改变,至少在膨胀首选项XML所使用的方面是如此。请确保您完全拥有我引用的构造函数(当然,用您自己的类名替换)。@dmon,commonware:添加了首选项的相关部分。在我看来,我已经有了所有需要的代码…天哪,我太笨了^^^我记得几天前我添加另一个方法存根时Eclipse做了一些自动修复,但我忘了删除它。。。谢谢!不过,奇怪的是,这对1.5来说没有问题?!我在某个地方读到一个错误,它允许在1.5中实例化抽象类,但我想知道为什么他们从来没有在这个版本中修复它…同意,1.5位是奇怪的。你知道这是我第二次遇到这个答案(第一次投了赞成票)——希望我第三次不需要它:D
public TimePreference(Context ctxt, AttributeSet attrs)
public abstract class PasswordProtectedEditTextPreference