0

I'm coming to think my problem is with my preferences not being able to load in Fragment. why i cannot access them. Here is My screenshot of error:

logcat:

https://i.stack.imgur.com/UeTG2.png

SettingsActivity.java

package com.example.poo.sunshine;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

public class SettingsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(R.id.container, new MyPreferenceFragment())
            .commit();
}

public static class MyPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener 
{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Add 'general' preferences, defined in the XML file
        addPreferencesFromResource(R.xml.pref_general);

        // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
        // updated when the preference changes.                

    PreferenceManager.getDefaultSharedPreferences(getActivity())
    .registerOnSharedPreferenceChangeListener(this);

    } 

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

       Preference locationPref = findPreference(getString(R.string.pref_location_key));

       Preference unitsPref = findPreference(getString(R.string.pref_units_key));

       prefChanged(sharedPreferences, locationPref, key);

       prefChanged(sharedPreferences, unitsPref, key);
    }

     private void prefChanged(SharedPreferences sharedPreferences, Preference pref, String key) {

        if (sharedPreferences instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list (since they have separate labels/values).

            ListPreference listPreference = (ListPreference) sharedPreferences;

            int prefIndex = listPreference.findIndexOfValue(key);

            if (prefIndex >= 0) {
                pref.setSummary(listPreference.getEntries()[prefIndex]);
            } else {
                pref.setSummary(sharedPreferences.getString(key, ""));

            }
        }
    }
}
}

So any help would be appreciated! I'm trying to access below preferences in MyPreferenceFragment class but cannot. My app crashes when I press on Setting option....

pref_general

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <EditTextPreference
    android:defaultValue="@string/pref_location_default"
    android:inputType="text"
    android:key="@string/pref_location_key"
    android:singleLine="true"
    android:title="@string/pref_location_label"/>

 <ListPreference
    android:title="@string/pref_units_label"
    android:key="@string/pref_units_key"
    android:defaultValue="@string/pref_units_metric"
    android:entryValues="@array/pref_units_values"
    android:entries="@array/pref_units_options"/>

</PreferenceScreen>

Here is My application ScreenShot

enter image description here

strings.xml

    <string name="pref_location_label">Location</string>

    <!-- Key name for storing location in SharedPreferences [CHAR LIMIT=NONE]->

    <string name="pref_location_key" translatable=`enter code here`"false">location</string>

    <!-- Default postal code for location preference [CHAR LIMIT=NONE] -->
    <string name="pref_location_default" translatable="false">94043</string>
ArK
  • 20,698
  • 67
  • 109
  • 136
pooja gajera
  • 373
  • 4
  • 20

1 Answers1

0

You can get string from resource by following:

getResources().getString(R.string.pref_location_key);

Not:

getString(R.string.pref_location_key)

More detail: Android: How do I get string from resources using its name?

Community
  • 1
  • 1
xxx
  • 3,315
  • 5
  • 21
  • 40