First - sorry for my english;) I'am writing profile manager for Android and i want getString from a few SharedPreferences file and create listView. It's part of my code:

private static final String PN = "profile_name";
private EditTextPreference editText;
private SharedPreferences preferences;

public class Profile_Preferences extends PreferenceActivity {

...

private void SavePreferences() {

    String text= editText.getText().toString();
    preferences = getSharedPreferences("Profile_" + text, Activity.MODE_PRIVATE);  //Here we created SharedPreferences file with name "Profile_"+ this what user write in editText

    SharedPreferences.Editor preferencesEditor = preferences.edit();
    preferencesEditor.putString(PN, editText.getText());
    preferencesEditor.commit();

Ok. The user was doing a few profile, so we have file for example: Profile_home.xml, Profile_work.xml, Profile_something.xml. Now i wan't create listView with profile name from this file. It's my next Activity:

public class Tab_profiles extends ListActivity {

ArrayList<String> listItems = new ArrayList<String>();  
ArrayAdapter<String> adapter; 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, listItems);
    setListAdapter(adapter);
 ...

public void LoadList() {        

 //It's a good idea to getString "profile_name" form every xml file with name "Profile_" + "" ? 
    preferences = getSharedPreferences("Profile_"+ "", Activity.MODE_PRIVATE);

    String take_profile_name = preferences.getString("profile_name", null);
    listItems.add(take_profile_name );
    adapter.notifyDataSetChanged();

}

But this doesn't work... logcat log:

FATAL EXCEPTION: MAIN 
java.land.Null.PointerException
at android.widget.ArrayAdaper.createViewFromResource(ArrayAdapter.java:355)
...

I don't now whoat it's wrong...

Please help my:) Thank you for any answers and sorry for my errors in writing and code;)

有帮助吗?

解决方案

There are a few problems in your code:

String text = editText.getText().toString();
preferences = getSharedPreferences("Profile_" + text, Activity.MODE_PRIVATE);

SharedPreferences.Editor preferencesEditor = preferences.edit();
preferencesEditor.putString(PN, editText.getText());

If a user types "home" in the EditText, you create a preference file called "Profile_home" and save its name in the same file? You need to save the user generated file names in a different file which has a name you know, so you can access it in your code.

Second problem:

preferences = getSharedPreferences("Profile_" + "", Activity.MODE_PRIVATE);
String take_profile_name = preferences.getString("profile_name", null);

You're trying to open "Profile_" settings. This file does not exist(?). The second parameter of getString must not be null, otherwise you'll add a null-reference to your list of string, which fails. Replace it with an empty string "".

Edit: Here's a little workaround to get all custom named preferences:

private void SavePreferences() {
    String text = editText.getText().toString();
    preferences = getSharedPreferences("ProfileNames", Activity.MODE_PRIVATE);
    SharedPreferences.Editor preferencesEditor = preferences.edit();
    // increment index by 1
    preferencesEditor.putInt("profile_count", preferences.getInt("profile_count", 0) + 1);
    // save new name in ProfileNames.xml with key "name[index]"
    preferencesEditor.putString("name" + (preferences.getInt("profile_count", 0) + 1), editText.getText());
    preferencesEditor.commit();
}

public void LoadList() {        
    preferences = getSharedPreferences("ProfileNames", Activity.MODE_PRIVATE);
    List<String> profileNames = new LinkedList<String>();
    int profileCount = preferences.getInt("profile_count", 0);
    for (int i = 1; i <= profileCount; i++) {
        profileNames.add(preferences.getString(name + i, ""));
    }
    listItems.addAll(profileNames);
    adapter.notifyDataSetChanged();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top