2016-11-08 6 views
1

私はユーザーから文字列を受け取り、後でアプリを閉じたときに使用しようとしています。アプリがバックグラウンドのときに動作しますが、閉じるときに文字列を失いますapp..isこれのようにする方法があるか、SharedPreferenceを使用する必要があります。もし私がそれを使用しなければならない場合は、私が試して失敗した理由を説明してください。アプリが閉じていても文字列を保存する

これはの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; 
} 

から文字列への私のMainActivityで私のコードのアプリのクラッシュであると

「インターフェイスメソッドを呼び出そうと「はjava.lang.String型android.contentを与えます.SharedPreferences.getString(java.lang.String、java.lang.String) 'nullオブジェクトリファレンスで "

この行で

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

これは私はあなたがそれをretriveする

SharedPreferences sharedpreferences = getSharedPreferences("MyPREFERENCES",Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedpreferences.edit(); 
editor.putString("TAG",reminder); 
editor.commit(); 

を使用することができます

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(); 
} 
+0

から借りgetString("TAG", "")

EDITを使用することができますが、[としてクラス全体(複数可)を表示できますmcve]のコードサンプルだけでなく、 'sharedPreferences'は何とか初期化されていないため、nullです。 –

+0

私は私の全体のMainActivityクラスを追加しました。私はちょうど私の質問に関連するスニペットを投稿していました。 –

+0

全部を追加しないで、そのリンクを読んでください。追加したコードのすべてがSharedPreferencesに関連しているわけではありません。 –

答えて

1

について

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()のメソッドを呼び出したどこ、あなたは活動の外SharedPreferencesがNULLであるとしてそれを行うことはできません。

私はあなたがnew MainActivity()を作ったとし、getRem()と書かれていると思いますか?

あなたが利用可能Contextから再びSharedPreferencesを取得する必要があり、その後、あなたは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", ""); 
    } 

} 
+0

はい、まさに私がやったことです:D –

+0

喜んで聞いてください。あなたは[任意の答えを受け入れる]であなたの感謝を表示することができます –

+0

あなたの助けとあなたの時間のためにそんなに感謝します。 –

1

メソッドを呼び出すクラスである:

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

[更新] SharedPreferences

+0

私はそれを試して、アプリはまだクラッシュ..私は新しいコードを追加..何かが間違っている? –

+0

public String getRem(){ reminder = ""; reminder = sharedPreferences.getString( "TAG"、 ""); リマインダーを返す。 } – Tefa

+0

これが機能しない場合は、このアクティビティのコード全体を入力してください。 – Tefa

関連する問題