-5

私の論理Main.java ファイルから textView を取得しようとしていますが、通知をクリックすると、それが出力されます。そのために sharedPreferences を使用することを知っています。私はそれをしました。ただし、NullPointerException をスローし続けますが、NotificationPage.java クラスで呼び出す必要があるすべてを呼び出したので、正確に何が間違っているのかわかりません。どんな助けでも大歓迎です。

これは私の NotificationPage.java クラスです

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;


public class NotificationPage extends Activity {
SharedPreferences sp;
SharedPreferences.Editor edit;
TextView arrival;
TextView notifdate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notification);
    notifdate=(TextView)findViewById(R.id.textView5);
    arrival=(TextView)findViewById(R.id.textView4);
    sp=getSharedPreferences(arr, MODE_PRIVATE);
    edit=sp.edit();
    sp.getString(arr, null);
    edit=sp.edit();
    edit.putString(arr,sp.getString(arr,null));
    notifdate.setText("Hey!" + sp.getString(arr,null));
    edit.apply();
}

これは、他の Java ファイルから共有設定を取得するコード行です。

arr=arrival.getText().toString();
sp=getSharedPreferences(arr,MODE_PRIVATE);
4

3 に答える 3

1

private final String SP_FILE = "my_shared_preferences";

共有設定の使い方がわかりません。共有プリファレンス ガイドを参照してください。

Shared Preferences を使用するには、読み取りまたは書き込みの 2 つの方法があります。

共有設定に書き込む場合は、まず名前を付けます。

public static final String SP_FILE_NAME = "my_shared_preferences";

SharedPreferences sp = getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);
sp.edit().putString("key", "value").apply();

1回の申請につき1回の編集があります。間に好きなものを入れることができます。

値を読み取るには:

SharedPreferences sp = getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);
String value = sp.getString("key", "defaultValue");

コードのどこが間違っているかを理解していただければ幸いです。

于 2015-09-10T01:57:35.610 に答える