Starting fragment on button click in DialogPreference

784 Views Asked by At

I have a PreferenceActivity from which I am trying to create a dialogpreference on click of preference. I first tried to keep my DialogPreference class (MyPreferencefragment) outside the PreferenceActivity class and the layout was

  <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"  />

This was fine but my dialogpreference has a button, on click of which I need to open a fragment. How can this be achieved? I do not have FragmentManager in Dialogpreference.

I tried creating DialogPreference as inner class in my PreferenceActivity but the layout of dialog preference came out to be as following.

 <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"  />

This throws an exception

    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)

This is useless as DialogPreference is an abstract class.

I just have to start a fragment on click of button from dialogpreference. How can this be achieved? Any suggestion would be helpful. Thanks in advance.

Edit:: Adding the whole stack

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

My major concerns are:

  1. Open custom dialogpreference on click of preference

  2. Open a fragment on click of a button in custom dialogpreference (There are three buttons in custom dialogpreference)

I can achieve 1 by not using dialogpreference as an inner class as it is giving me the abovementioned stacktrace. But how do I achieve second point?

1

There are 1 best solutions below

1
On

You need to have a container (typically a frame layout), then you swap the fragment into that container with a fragment transaction. That exception alone isn't very descriptive, I'm not sure what you did before you got it - you need at least the two-param constructor present to use fragment via XML, otherwise make sure you have an empty constructor if you've tried to call it that way (in code).

Post the rest of your stack trace, there should be a caused-by.

getSupportFragmentManager().beginTransaction().replace(R.id.your_container_id, new YourFragment()).commit();