Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 即使应用程序已关闭,也会存储字符串_Android_String_Nullpointerexception_Sharedpreferences - Fatal编程技术网

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

Android 即使应用程序已关闭,也会存储字符串,android,string,nullpointerexception,sharedpreferences,Android,String,Nullpointerexception,Sharedpreferences,我正在尝试从用户那里获取字符串,并在应用程序关闭后使用它。现在它只在应用程序处于后台时工作,但在关闭应用程序时我丢失了字符串。有没有办法做到这一点,或者我必须使用SharedReference,如果我必须使用它,请解释如何使用它,因为我尝试了,但失败了。非常感谢 这是我在MainActivity中的代码,用于从EditText中删除字符串 public class MainActivity extends AppCompatActivity { private SharedPreferences

我正在尝试从用户那里获取字符串,并在应用程序关闭后使用它。现在它只在应用程序处于后台时工作,但在关闭应用程序时我丢失了字符串。有没有办法做到这一点,或者我必须使用SharedReference,如果我必须使用它,请解释如何使用它,因为我尝试了,但失败了。非常感谢

这是我在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.SharedReferences.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();
}
你可以用

要检索它,请执行以下操作:

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", ""); 
}

有关

的更多信息无论您在哪里调用
getRem()方法
,您都不能在活动之外执行此操作,因为SharedReference为空

比如,我假设您创建了一个
新的MainActivity()
,然后调用了
getRem()

您需要从可用的
上下文中再次获取SharedReferences,然后可以使用
getString(“TAG”,“”)

编辑借用自


我尝试了它,但应用程序仍然崩溃。我添加了新代码。我做错了什么?公共字符串getRem(){rementer=”“;rementer=SharedReferences.getString(“TAG”),return rementer;}如果它不起作用,请将此活动的整个代码都放进去。我看到您声明了gerRem()方法,但您没有使用它:]应用程序如何崩溃?我在扩展BroadcastReceiver的另一个类中调用它。我添加了它。您可以将整个类显示为一个类,而不仅仅是这些小代码示例吗
SharedReferences
为空,因为它没有以某种方式初始化。我添加了整个MainActivity类。我只是发布了与我的问题相关的片段。请不要添加全部内容,请阅读该链接。不是所有你添加的代码都与SharedReferences有关。很高兴听到这个消息。你可以通过非常感谢你的帮助和你的时间来表达你的谢意……没错:)
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", ""); 
}
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", "");
    }

}