这里是造成我的问题代码:SharedPreferences不保存价值
public class MainActivity extends Activity {
private TextView lblName;
private View.OnClickListener droidTapListener;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
boolean prefsShown = prefs.getBoolean("haveShownPrefs", false);
setContentView(R.layout.activity_main);
if (!prefsShown){
//launch the preferences activity
Intent i = new Intent(getApplicationContext(), Preferences.class);
startActivity(i);
}
init(prefs);
}
private void init(SharedPreferences prefs) {
lblName = (TextView) findViewById(R.id.lblName);
lblName.setText(prefs.getString("userName",""));
}
}
public class Preferences extends Activity {
private EditText txtName;
private Button btnDone;
private View.OnClickListener droidTapListener;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);
SharedPreferences prefs = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor ed = prefs.edit();
ed.putBoolean("haveShownPrefs", true);
ed.commit();
init(prefs, ed);
}
private void init(SharedPreferences prefs, SharedPreferences.Editor ed) {
btnDone = (Button) findViewById(R.id.btnDone);
txtName = (EditText) findViewById(R.id.txtName);
ed.putString("userName", txtName.getText().toString());
ed.commit();
droidTapListener = new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
};
btnDone.setOnClickListener(droidTapListener);
}
}
下面是情况下,XML我失去了一些东西:
的preferences.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Preferences">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/name"
android:id="@+id/lblName"
android:layout_column="3"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/txtName"
android:layout_column="9"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/done"
android:id="@+id/btnDone"
android:layout_column="10"/>
</TableRow>
</TableLayout>
activity_main.xml中:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Preferences">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/lblName"
android:layout_column="5"/>
</TableRow>
</TableLayout>
的问题是,当我去打印lblName它是空白的名字在MainActivity,我就Prefereces做测试:
public void onClick(View v) {
btnDone.setText(txtName.getText().toString());
}
并能正常工作,所以这个问题是与SharedPreferences。有任何想法吗?提前致谢!
您是否曾手动检查过preferences.xml?使用浏览器 – Oli
@Oli我不确定你的意思,如果我用webbrowser打开preferences.xml,那么它只是显示我的xml代码,并且确实检查了它的错误。 我已经将xml添加到问题中,也许我错过了一些东西。 – Aterin