【发布时间】:2019-11-26 02:38:08
【问题描述】:
我正在尝试从 getIntent 字符串中保存 textview。当我关闭应用程序,然后再次打开时,textview 为空。我用过很多方法,比如onSaveInstance、SharedPreference,但都失败了。
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
jamShubuh = findViewById(R.id.jamShubuhTextView);
jamDzuhur = findViewById(R.id.jamDzuhurTextView);
jamAshar = findViewById(R.id.jamAsharTextView);
getShubuh = getIntent().getStringExtra("getShubuh");
getDzuhur = getIntent().getStringExtra("getDzuhur");
getAshar = getIntent().getStringExtra("getAshar");
jamShubuh.setText(getShubuh);
jamDzuhur.setText(getDzuhur);
jamAshar.setText(getAshar);
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("shubuh", getShubuh);
editor.putString("dzuhur", getDzuhur);
editor.putString("ashar", getAshar);
retriveData();
private void retriveData() {
SharedPreferences getData = getPreferences(Context.MODE_PRIVATE);
getData.getString("shubuh", null);
getData.getString("dzuhur", null);
getData.getString("ashar", null);
}
【问题讨论】:
-
这不是minimal, reproducible example,您显示了一些不显示 R 声明的随机代码。换句话说,您使用了一个未显示的名为 R 的变量,这可能导致上述错误。另外,您能否包含一条错误消息,其中包含指向您描述的导致问题的行的指针?
-
请edit 向我们展示您对
SharedPreferences的尝试。这似乎是只保留三个Strings 的合适选择。 -
@KnowNoTrend
R是 Android 中自动生成的资源类。这只是一堆final static字段。通常不需要在问题中包含它。 -
@MikeM。等你
-
@MikeM。问题已编辑
标签: java android textview sharedpreferences onsaveinstancestate