我在SharedPreference中存储了值,现在我想访问Fragment中的所有内容。当我试图运行我的应用程序时,它崩溃了。
public class Credentials extends Fragment {
Button submit, change;
EditText user, id;
Context ctx;
Context context = getActivity();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View windows = inflater.inflate(R.layout.credential, container, false);
((TextView)windows.findViewById(R.id.textView)).setText("Credentials");
submit = ((Button)windows.findViewById(R.id.submit));
change = ((Button)windows.findViewById(R.id.change));
user = ((EditText)windows.findViewById(R.id.username));
id = ((EditText)windows.findViewById(R.id.Useremail));
SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
String username = "";
String email = "";
pref.getString("username", username);
pref.getString("email", email);
Toast.makeText(getActivity(), "Pressed", Toast.LENGTH_SHORT).show();
user.setText(username);
id.setText(email);
}
答案 0 :(得分:2)
我为SharedPreference创建了一个Util类,希望它能帮到你。
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPrefrenceUtils {
public static final String SHARED_PREFERENCE_TAG="TAG_NAME";
private SharedPrefrenceUtils(){
throw new AssertionError();
}
public static String getString(Context mContext, String key){
SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
return pref.getString(key, null);
}
public static String getString(Context mContext, String key, String defaultValue){
SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
return pref.getString(key, defaultValue);
}
public static void putString(Context mContext, String key, String value ){
SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString(key, value);
editor.commit();
}
public static int getInt(Context mContext, String key){
SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
return pref.getInt(key, 0);
}
public static int getInt(Context mContext, String key, int defaultValue){
SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
return pref.getInt(key, defaultValue);
}
public static void putInt(Context mContext, String key, int value ){
SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt(key, value);
editor.commit();
}
public static long getLong(Context mContext, String key){
SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
return pref.getLong(key, 0);
}
public static long getLong(Context mContext, String key, long defaultValue){
SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
return pref.getLong(key, defaultValue);
}
public static void putLong(Context mContext, String key, long value ){
SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putLong(key, value);
editor.commit();
}
public static boolean getBoolean(Context mContext, String key){
SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
return pref.getBoolean(key, false);
}
public static boolean getBoolean(Context mContext, String key, boolean defaultValue){
SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
return pref.getBoolean(key, defaultValue);
}
public static void putBoolean(Context mContext, String key, boolean value ){
SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(key, value);
editor.commit();
}
public static void remove(Context mContext, String key){
SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.remove(key);
editor.commit();
}
public static void clear(Context mContext){
SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
editor.commit();
}
}
现在存储字符串值:在片段中使用以下语句
SharedPrefrenceUtils.putString(mContext,"username", "1234");
SharedPrefrenceUtils.putString(mContext,"email", "xyz@gmail.com");
获取字符串值:在片段中使用以下语句
String userName = SharedPrefrenceUtils.getString(mContext,"username");
String email = SharedPrefrenceUtils.getString(mContext,"email");
您可以从应用程序的任何片段或活动中存储和检索任何其他值。
答案 1 :(得分:0)
您的接收方法存在问题。
public abstract String getString(String key,String defValue)
从首选项中检索字符串值。
<强>参数强>
键:要检索的首选项的名称 defValue :如果此首选项不存在,则返回值。
退货
返回首选项值(如果存在)或defValue。抛出 如果存在不具有此名称的首选项,则为ClassCastException 一个字符串。
因此,当您使用pref.getString()
方法时,您必须将返回值存储为某些内容。
尝试以下示例。
String username = pref.getString("username", "EMPTY");
Toast.makeText(getActivity(), "Value : "+username, Toast.LENGTH_SHORT).show();