我正在实施共享偏好设置,并且我想要显示以前保存的内容。为此,即使使用null首选项文件,我也试图设置编辑框文本。 我不确定是否可能。
public class PerfilActivity extends Activity {
public static final String PERFIL_FILE = "MyPreferencesFile";
private Button btn_historico, btn_actualiza;
private EditText ed_nome, ed_altura, ed_peso, ed_gordura;
private SharedPreferences settings;
private SharedPreferences.Editor editor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_perfil);
btn_actualiza = (Button)findViewById(id.btn_atualizar);
btn_historico = (Button)findViewById(id.btn_verHistorico);
ed_nome = (EditText)findViewById(id.ed_nome);
ed_altura = (EditText)findViewById(id.ed_altura);
ed_peso = (EditText)findViewById(id.ed_peso);
ed_gordura = (EditText)findViewById(id.ed_gorduraCorporal);
ed_nome.setText(settings.getString("nome", "(Insira o nome)"));
ed_altura.setText(settings.getInt("altura", 1232));
ed_peso.setText(settings.getInt("peso", 222));
ed_gordura.setText(settings.getInt("gordura", 123));
btn_actualiza.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
settings = getSharedPreferences(PERFIL_FILE, 0);
editor = settings.edit();
editor.putString("nome", ed_nome.getText().toString());
editor.putInt("altura", Integer.parseInt(ed_altura.getText().toString()));
editor.putInt("peso", Integer.parseInt(ed_peso.getText().toString()));
editor.putInt("gordura", Integer.parseInt(ed_gordura.getText().toString()));
showToast("Valores actualizados com sucesso!", 1);
editor.commit();
}
});
}
}
logcat的
01-03 04:06:24.505: E/AndroidRuntime(31419): FATAL EXCEPTION: main
01-03 04:06:24.505: E/AndroidRuntime(31419): Process: com.AMOV.mr.fit, PID: 31419
01-03 04:06:24.505: E/AndroidRuntime(31419): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.AMOV.mr.fit/com.AMOV.mr.fit.PerfilActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x7b
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2045)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:549)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.view.View.performClick(View.java:4445)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.view.View$PerformClick.run(View.java:18446)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.os.Handler.handleCallback(Handler.java:733)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.os.Handler.dispatchMessage(Handler.java:95)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.os.Looper.loop(Looper.java:136)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.app.ActivityThread.main(ActivityThread.java:5146)
01-03 04:06:24.505: E/AndroidRuntime(31419): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 04:06:24.505: E/AndroidRuntime(31419): at java.lang.reflect.Method.invoke(Method.java:515)
01-03 04:06:24.505: E/AndroidRuntime(31419): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
01-03 04:06:24.505: E/AndroidRuntime(31419): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
01-03 04:06:24.505: E/AndroidRuntime(31419): at dalvik.system.NativeStart.main(Native Method)
01-03 04:06:24.505: E/AndroidRuntime(31419): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7b
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.content.res.Resources.getText(Resources.java:265)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.widget.TextView.setText(TextView.java:3888)
01-03 04:06:24.505: E/AndroidRuntime(31419): at com.AMOV.mr.fit.PerfilActivity.onCreate(PerfilActivity.java:39)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.app.Activity.performCreate(Activity.java:5231)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-03 04:06:24.505: E/AndroidRuntime(31419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-03 04:06:24.495: E/ActivityThread(31419): Failed to find resource
01-03 04:06:24.495: E/ActivityThread(31419): android.content.res.Resources$NotFoundException: String resource ID #0x7b
01-03 04:06:24.495: E/ActivityThread(31419): at android.content.res.Resources.getText(Resources.java:265)
01-03 04:06:24.495: E/ActivityThread(31419): at android.widget.TextView.setText(TextView.java:3888)
01-03 04:06:24.495: E/ActivityThread(31419): at com.AMOV.mr.fit.PerfilActivity.onCreate(PerfilActivity.java:39)
01-03 04:06:24.495: E/ActivityThread(31419): at android.app.Activity.performCreate(Activity.java:5231)
01-03 04:06:24.495: E/ActivityThread(31419): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-03 04:06:24.495: E/ActivityThread(31419): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-03 04:06:24.495: E/ActivityThread(31419): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2045)
01-03 04:06:24.495: E/ActivityThread(31419): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
01-03 04:06:24.495: E/ActivityThread(31419): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
01-03 04:06:24.495: E/ActivityThread(31419): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
01-03 04:06:24.495: E/ActivityThread(31419): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
01-03 04:06:24.495: E/ActivityThread(31419): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
01-03 04:06:24.495: E/ActivityThread(31419): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:549)
01-03 04:06:24.495: E/ActivityThread(31419): at android.view.View.performClick(View.java:4445)
01-03 04:06:24.495: E/ActivityThread(31419): at android.view.View$PerformClick.run(View.java:18446)
01-03 04:06:24.495: E/ActivityThread(31419): at android.os.Handler.handleCallback(Handler.java:733)
01-03 04:06:24.495: E/ActivityThread(31419): at android.os.Handler.dispatchMessage(Handler.java:95)
01-03 04:06:24.495: E/ActivityThread(31419): at android.os.Looper.loop(Looper.java:136)
01-03 04:06:24.495: E/ActivityThread(31419): at android.app.ActivityThread.main(ActivityThread.java:5146)
01-03 04:06:24.495: E/ActivityThread(31419): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 04:06:24.495: E/ActivityThread(31419): at java.lang.reflect.Method.invoke(Method.java:515)
01-03 04:06:24.495: E/ActivityThread(31419): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
01-03 04:06:24.495: E/ActivityThread(31419): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
01-03 04:06:24.495: E/ActivityThread(31419): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
Resources $ NotFoundException:字符串资源ID#0x7b
因为您正在将Integer
传递给EditText.setText
方法。使用String.valueOf(<Int value>)
在EditTexts中显示值:
ed_altura.setText(String.valueOf(settings.getInt("altura", 1232)));
ed_peso.setText(String.valueOf(settings.getInt("peso", 222)));
ed_gordura.setText(String.valueOf(settings.getInt("gordura", 123)));
答案 1 :(得分:2)
在设置为EditText
之前,必须解析值,因为值必须为字符串类型
ed_altura.setText(settings.getInt("altura", 1232));
ed_peso.setText(settings.getInt("peso", 222));
ed_gordura.setText(settings.getInt("gordura", 123));
替换为
ed_altura.setText(String.valueOf(settings.getInt("altura", 1232)));
ed_peso.setText(String.valueOf(settings.getInt("peso", 222)));
ed_gordura.setText(String.valueOf(settings.getInt("gordura", 123)));
检查一下它可能会帮助你