在使用InstantiationException的EditText preference的子类 [英] InstantiationException when using subclass of EditTextPreference
问题描述
假设,我有的EditText preference
的子类,称为PasswordProtectedEditText preference。这基本上子显示密码对话框才可以编辑由的EditText preference
的自己对话的preference。
现在我定义了相应的preferences.xml这样的preference:
< edu.myproject.pw prefs.PasswordProtectedEditText preference机器人:关键=pref_password
机器人:标题=@字符串/ pref_password_title机器人:总结=@字符串/ pref_password_summary
机器人:DIALOGTITLE =@字符串/ pref_password_dialog_title机器人:dialogMessage =@字符串/ pref_password_dialog_message>
< /edu.myproject.pw$p$pfs.PasswordProtectedEditText$p$pference>
然后我在申请我的 preferenceActivity
的子preferences.xml通过调用
添加preferencesFromResource(R.xml preferences);
在的onCreate()
。这一切都采用了Android 1.5能正常工作。然而,当我使用更高的Android版本,我得到以下堆栈跟踪:
致命异常:主要
了java.lang.RuntimeException:无法启动活动ComponentInfo {edu.myproject / edu.myproject preferences}:android.view.InflateException:二进制XML文件行#25:错误充气类java.lang.reflect.Constructor中
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
在android.app.ActivityThread.access $ 2300(ActivityThread.java:135)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2136)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:144)
在android.app.ActivityThread.main(ActivityThread.java:4937)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:521)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
在dalvik.system.NativeStart.main(本机方法)
android.view.InflateException:引起二进制XML文件行#25:错误充气类java.lang.reflect.Constructor中
在Android系统。preference.GenericInflater.createItem(GenericInflater.java:397)
在Android系统。preference.GenericInflater.createItemFromTag(GenericInflater.java:430)
在Android系统。preference.GenericInflater.rInflate(GenericInflater.java:481)
在Android系统。preference.GenericInflater.rInflate(GenericInflater.java:493)
在Android系统。preference.GenericInflater.inflate(GenericInflater.java:326)
在Android系统。preference.GenericInflater.inflate(GenericInflater.java:263)
在Android的preference preferenceManager.inflateFromResource(preferenceManager.java:254)。
。在安卓preference preferenceActivity.add preferencesFromResource(preferenceActivity.java:268)
在edu.myproject preferences.onCreate(preferences.java:50)。
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
... 11更多
java.lang.InstantiationException:引起edu.myproject.pw prefs.PasswordProtectedEditText preference
在java.lang.reflect.Constructor.constructNative(本机方法)
在java.lang.reflect.Constructor.newInstance(Constructor.java:446)
在Android系统。preference.GenericInflater.createItem(GenericInflater.java:383)
...... 21多
任何想法,这里发生了什么?
编辑:#25从堆栈跟踪对应于preferences.xml的PasswordProtectedEditText preference
编辑:这是我PasswordProtectedEditText preference:
包edu.myproject.pw preFS;进口edu.myproject.R;
进口android.app.AlertDialog;
进口android.content.Context;
进口android.content.DialogInterface;
进口的Android preference.EditText preference。
进口android.util.AttributeSet;
进口android.view.Gravity;
进口android.widget.EditText;
进口android.widget.LinearLayout;公共抽象类PasswordProtectedEditText preference延伸的EditText $ P $ {pference 公共PasswordProtectedEditText preference(上下文的背景下){
超级(上下文);
// TODO自动生成构造函数存根
} 公共PasswordProtectedEditText preference(上下文的背景下,
AttributeSet中的ATTRS){
超(背景下,ATTRS);
// TODO自动生成构造函数存根
} 公共PasswordProtectedEditText preference(上下文的背景下,
ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
// TODO自动生成构造函数存根
}
//进一步非实例相关的code
}
您创建了一个的摘要的类:
公共抽象类PasswordProtectedEditText preference
难怪它不能被实例化; - )
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
}
You created an abstract class:
public abstract class PasswordProtectedEditTextPreference
No wonder it can't be instantiated ;-)
这篇关于在使用InstantiationException的EditText preference的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!