因此,对于我正在开发的应用程序,我想使用共享偏好来保存数据,在这种情况下,我想保存用户选择的语言。这是通过一个数字来完成的,它会保存这个但是当我尝试在我的菜单页面上使用这个值时它会一直崩溃
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
int taal=sp.getInt("taal", 4);
if(taal==1){
String tekst="Nieuw Spel";
button1.setText(tekst);
//button1.setText("Nieuw Spel");
//button2.setText("Statestieken");
//button3.setText("Opties");
//Languege.MenuText(button1, button2, button3, taal);
button1.setText(taal);
button4.setVisibility(Button.GONE);
button5.setVisibility(Button.GONE);
}
这回来了,因为错误但是我真的找不到我做错了什么
01-20 16:11:05.535: E/AndroidRuntime(4308): FATAL EXCEPTION: main
01-20 16:11:05.535: E/AndroidRuntime(4308): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stevedc.thinklogic/com.stevedc.thinklogic.MainMenu}: android.content.res.Resources$NotFoundException: String resource ID #0x1
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.os.Looper.loop(Looper.java:137)
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-20 16:11:05.535: E/AndroidRuntime(4308): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 16:11:05.535: E/AndroidRuntime(4308): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 16:11:05.535: E/AndroidRuntime(4308): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-20 16:11:05.535: E/AndroidRuntime(4308): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-20 16:11:05.535: E/AndroidRuntime(4308): at dalvik.system.NativeStart.main(Native Method)
01-20 16:11:05.535: E/AndroidRuntime(4308): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.content.res.Resources.getText(Resources.java:247)
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.widget.TextView.setText(TextView.java:3495)
01-20 16:11:05.535: E/AndroidRuntime(4308): at com.stevedc.thinklogic.MainMenu.onCreate(MainMenu.java:33)
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.app.Activity.performCreate(Activity.java:4465)
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-20 16:11:05.535: E/AndroidRuntime(4308): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
答案 0 :(得分:3)
更改
button1.setText(taal);
所以它是
button1.setText(String.valueOf(taal));
通过传递整数(taal
),Android会尝试查找资源ID并失败。