I am new to android and following the Udacity Android developer course to learn. I am on chapter 3 where they teach about the adding the settings in your app. I have added the SettingActivity from android studio. When I click on settings menu, my app crashes. This is my pref_general.xml file
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<EditTextPreference
android:key="@string/pref_location_key"
android:defaultValue="@string/pref_location_default"
android:maxLines="1"
android:inputType="text"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_location_label" />
I have made these changes in SettingActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));}
I am getting following exception when I click the Setting menu
Process: com.example.android.sunshine.app, PID: 10131
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.sunshine.app/com.example.android.sunshine.app.SettingsActivity}: java.lang.RuntimeException: Modern two-pane PreferenceActivity requires use of a PreferenceFragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.RuntimeException: Modern two-pane PreferenceActivity requires use of a PreferenceFragment
at android.preference.PreferenceActivity.requirePreferenceManager(PreferenceActivity.java:1441)
at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1511)
at com.example.android.sunshine.app.SettingsActivity.onCreate(SettingsActivity.java:124)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Any help? Sorry if its very naive thing
I was able to solve this.
I was changing the OnCreate of my SettingActivity instead of changing my OnCreate of SettingFragment which is static private class of SettingActivity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With