Android共享首选项:如何从String设置中获取值?

时间:2016-09-18 09:21:26

标签: android android-studio sharedpreferences

我创建了一个名为'nameSet'的字符串集,其中保存了许多名称。

Set<String> nameSet=new HashSet<String>(); //declared as global

public void loadAllData()
{
    nameSet.add("Matt");
    nameSet.add("John");
    nameSet.add("Mike");
    nameSet.add("Gilbert");
    nameSet.add("Kyle");
    nameSet.add("Jim");
    editor.putStringSet("SET1",nameSet); 
    // do I need to declare 'SET1' as a String set beforehand?
    editor.commit();
}

现在,我只想从'SET1'中检索任何一个随机值或第一个值;我已经为它编写了代码,但它仍然给我错误:

public String getData()
{
    Set<String> name=new HashSet<String>();
    name=pref.getStringSet("SET1",null);
    if(name!=null)
    {
        for(String i:name)
        {
            PersonName=i; //does this line retrieve only one name?
        }
    }
    return PersonName; //PersonName is a String passed to another activity
}

错误Logcat:

FATAL EXCEPTION: main
                                                                                    Process: com.demosquareout.compaq.algarferrariapp, PID: 6500
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demosquareout.compaq.Pingapp/com.demosquareout.compaq.Pingapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void PingApp.fragments.PersonDetails.loadAllData()' on a null object reference
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
                                                                                        at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7331)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void PingApp.fragments.PersonDetails.loadAllData()' on a null object reference
                                                                                        at algarferrari.fragments.PersonList.onCreateView(PersonList.java:56)
                                                                                        at android.app.Fragment.performCreateView(Fragment.java:2281)
                                                                                        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:984)
                                                                                        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1164)
                                                                                        at android.app.BackStackRecord.run(BackStackRecord.java:793)
                                                                                        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1557)
                                                                                        at android.app.FragmentController.execPendingActions(FragmentController.java:326)
                                                                                        at android.app.Activity.performStart(Activity.java:6942)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3276)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
                                                                                        at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7331) 
                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

在'PersonDetails'活动的构造函数中,我调用了loadAllData()。请注意,我已更改了呼叫loadAllData()的位置。

public Person Details(Context context)
{
    this._context = context;
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    editor = pref.edit();
    loadAllData();
}

1 个答案:

答案 0 :(得分:0)

@ sam88,我对您的代码感到有些困惑,因为您在活动中的 onCreateView 中尝试了代码,可能是Activity出现问题框架。

我认为在您的代码中编辑器对象变为空。

尝试此代码可能会解决您的问题。

    SharedPreferences sharedpreferences = mContext.getSharedPreferences("preferences", Context.MODE_PRIVATE);

    SharedPreferences.Editor editor = sharedpreferences.edit();

    editor.putStringSet("SET1",nameSet); 

    editor.commit();

此处, mContext 是您活动的背景。