首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在片段内的SharedPreferences中获取pref_name的值?

如何在片段内的SharedPreferences中获取pref_name的值?
EN

Stack Overflow用户
提问于 2018-07-29 00:13:26
回答 2查看 198关注 0票数 -1

我一直在尝试从SharedPreferences中获取一个字符串形式的键的值,这样我就可以修改连接到片段的布局中的数据。

下面是我的OneFragment.java代码

public class OneFragment extends Fragment{

private TextView mText;

public OneFragment() {
    // Required empty public constructor
}

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



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_one, container, false);

    SharedPreferences prefs = getActivity().getSharedPreferences("EID", Context.MODE_PRIVATE);
     String value = prefs.getString("EID", "0");

    mText = (TextView) view.findViewById(R.id.textView2);
    mText.setText(value);
 return view;

  }
}

该应用程序表示,它在打开时已停止。我从Main activity中调用片段。

制片厂说:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.dell.tab2, PID: 3857
              java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.dell.tab2/com.example.dell.tab2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
                  at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:164)
                  at com.example.dell.tab2.MainActivity.<init>(MainActivity.java:31)
                  at java.lang.Class.newInstance(Native Method)
                  at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-29 00:44:34

如果您不能以片段形式获取此值,则可以从您的活动中获取此值。在片段中调用:

String value = ((YourActivityName)getActivity()).getValuePreference();

并在您的活动中添加方法:

public String getValuePreference(){
    SharedPreferences preferences = getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE);
    return preferences.getString("EID", "0");
}
票数 0
EN

Stack Overflow用户

发布于 2018-07-29 00:23:45

试试这个:

在片段中添加以下代码

  Context context;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.context = context;
}

onCreateView中,用context替换getActivity()

  SharedPreferences prefs = context.getSharedPreferences("EID", Context.MODE_PRIVATE);
 String value = prefs.getString("EID", "0");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51573103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档