我有一个PreferenceActivity,我试图在点击偏好时创建一个对话框。我首先尝试将我的DialogPreference类(MyPreferencefragment)保留在PreferenceActivity类之外,并且布局是
<com.example.second.MyPreferencefragment
android:dialogMessage="Demo"
android:key="resetDialog"
android:negativeButtonText="No"
android:persistent="false"
android:positiveButtonText="Yes"
android:summary="Place the time here"
android:title="Demo App" />
这很好但我的对话框有一个按钮,点击它我需要打开一个片段。怎么能实现这一目标?我在Dialogpreference中没有FragmentManager。
我尝试在PreferenceActivity中创建DialogPreference作为内部类,但对话框首选项的布局如下:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<DialogPreference class = "com.example.second.PrefsActivity$MyPreferencefragment"
android:dialogMessage="Demo"
android:key="resetDialog"
android:negativeButtonText="No"
android:persistent="false"
android:positiveButtonText="Yes"
android:summary="Place the time here"
android:title="Demo App" />
这会引发异常
Caused by: java.lang.InstantiationException: can't instantiate class android.preference.DialogPreference
01-15 05:54:01.427: E/AndroidRuntime(12843): at java.lang.reflect.Constructor.constructNative(Native Method)
这是没用的,因为DialogPreference是一个抽象类。
我只需要在对话框首选项上点击按钮即可启动片段。怎么能实现这一目标?任何建议都会有所帮助。提前谢谢。
编辑::添加整个堆栈
01-15 05:27:41.325: E/AndroidRuntime(8932): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.second/com.example.second.PrefsActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class java.lang.reflect.Constructor
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2253)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2311)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.app.ActivityThread.access$700(ActivityThread.java:152)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1438)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.os.Handler.dispatchMessage(Handler.java:102)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.os.Looper.loop(Looper.java:158)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.app.ActivityThread.main(ActivityThread.java:5236)
01-15 05:27:41.325: E/AndroidRuntime(8932): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 05:27:41.325: E/AndroidRuntime(8932): at java.lang.reflect.Method.invoke(Method.java:515)
01-15 05:27:41.325: E/AndroidRuntime(8932): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1257)
01-15 05:27:41.325: E/AndroidRuntime(8932): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1073)
01-15 05:27:41.325: E/AndroidRuntime(8932): at dalvik.system.NativeStart.main(Native Method)
01-15 05:27:41.325: E/AndroidRuntime(8932): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class java.lang.reflect.Constructor
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.preference.GenericInflater.createItem(GenericInflater.java:397)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1778)
01-15 05:27:41.325: E/AndroidRuntime(8932): at com.example.tapholdcustom.PrefsActivity.onCreate(PrefsActivity.java:38)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.app.Activity.performCreate(Activity.java:5389)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2217)
01-15 05:27:41.325: E/AndroidRuntime(8932): ... 11 more
01-15 05:27:41.325: E/AndroidRuntime(8932): Caused by: java.lang.InstantiationException: can't instantiate class android.preference.DialogPreference
01-15 05:27:41.325: E/AndroidRuntime(8932): at java.lang.reflect.Constructor.constructNative(Native Method)
01-15 05:27:41.325: E/AndroidRuntime(8932): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
01-15 05:27:41.325: E/AndroidRuntime(8932): at android.preference.GenericInflater.createItem(GenericInflater.java:383)
01-15 05:27:41.325: E/AndroidRuntime(8932): ... 22 more
我主要担心的是:
点击首选项打开自定义对话框
单击自定义对话框首选项中的按钮打开片段(自定义对话框首选项中有三个按钮)
我可以通过不使用dialogpreference作为内部类来实现1,因为它给了我上面提到的堆栈跟踪。但我如何实现第二点?
答案 0 :(得分:1)
您需要有一个容器(通常是框架布局),然后使用片段事务将片段交换到该容器中。单独的那个例外并不是非常具有描述性的,我不确定你在得到它之前做了什么 - 你至少需要存在的两个参数构造函数才能通过XML使用片段,否则请确保你有一个空的构造函数如果您试图以这种方式调用它(在代码中)。
发布堆栈跟踪的其余部分,应该有一个引起的。
getSupportFragmentManager().beginTransaction().replace(R.id.your_container_id, new YourFragment()).commit();