共享首选项上的空指针
Null pointer on shared preferences
我正在实施共享首选项,我想让之前保存的内容显示出来。为此,我试图设置编辑框文本,即使使用空首选项文件也是如此。
我不确定是否可行。
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();
}
});
}
}
日志猫
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.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.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)
Resources$NotFoundException: String resource 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)));
您必须在设置为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)));
检查这个可能对你有帮助
我正在实施共享首选项,我想让之前保存的内容显示出来。为此,我试图设置编辑框文本,即使使用空首选项文件也是如此。 我不确定是否可行。
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();
}
});
}
}
日志猫
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.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.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)
Resources$NotFoundException: String resource 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)));
您必须在设置为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)));
检查这个可能对你有帮助