即使应用程序已关闭,也会存储字符串

时间:2016-11-08 23:06:40

标签: android string nullpointerexception sharedpreferences

我正在尝试从用户获取字符串并在应用程序关闭后使用它..现在它适用于应用程序在后台但我在关闭应用程序时丢失字符串..有没有办法这样做或我必须使用SharedPreference,如果我必须使用它,请解释如何因为我尝试和失败..谢谢很多。

这是我的MainActivity中的代码,来自EditText的字符串

public class MainActivity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
private static String reminder;
 private EditText et;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    // initialize variables


    sharedPreferences = getSharedPreferences("MyPREFERENCES",Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor = sharedPreferences.edit();
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            et = (EditText) findViewById(R.id.Name);
            reminder = et.getText().toString();
            if(reminder == null){
                reminder = "TWEAK!";
            }

            editor.putString("TAG",reminder);
            editor.commit();
            // do stuff
}

// get the user's string
public String getRem() {
    reminder = sharedPreferences.getString("TAG", "");
    return reminder;
}

应用程序崩溃并提供

  

“尝试在空对象引用上调用接口方法'java.lang.String android.content.SharedPreferences.getString(java.lang.String,java.lang.String)'”

在这一行

reminder = sharedPreferences.getString("TAG", "");

这是我调用方法的类

public class Notifications extends BroadcastReceiver {
private String rem;

      // set notification
@Override
public void onReceive(Context context, Intent intent) {
    // object to access MainActivity methods
    MainActivity main = new MainActivity();
    rem = main.getRem();
}

2 个答案:

答案 0 :(得分:1)

您可以使用

sharedpreferences.getString("TAG","");

要追溯它:

public class Notifications extends BroadcastReceiver { 
private String rem;
 // set notification
 @Override
 public void onReceive(Context context, Intent intent) { 
// object to access MainActivity methods
 SharedPreferences sharedPreferences = context.getSharedPreferences("MyPREFERENCES",Context.MODE_PR‌​IVATE);
  rem = sharedPreferences.getString("TAG", ""); 
}

[更新]

$('.date').datepicker({
     showOn: 'focus',
     dateFormat: 'mm-dd-yy',
     changeMonth: true,
     changeYear: true,
     onSelect: function() {
         var event;
         if(typeof window.Event == "function"){
             event = new Event('change');
             this.dispatchEvent(event);
         }   else {
             event = document.createEvent('HTMLEvents');
             event.initEvent('change', false, false);
             this.dispatchEvent(event);
         }
     }
});

更多关于SharedPreferences

答案 1 :(得分:1)

无论您何时调用getRem()方法,都无法在Activity之外执行此操作,因为SharedPreferences为null。

就像,我假设你做了一个new MainActivity(),然后在那个叫getRem(),或许?

您需要从可用的Context再次获取SharedPreferences,然后才能使用getString("TAG", "")

编辑借鉴Shared preferences inside broadcastreceiver

public class Notifications extends BroadcastReceiver {

    private String rem;

    // set notification
    @Override
    public void onReceive(Context context, Intent intent) {
        setRem(context);
    }

    private void setRem(Context context) {
        SharedPreferences prefs = context.getSharedPreferences("MyPREFERENCES",Context.MODE_PRIVATE);
        rem = prefs.getString("TAG", "");
    }

}