我试图从我的 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);