gpt4 book ai didi

android - sharedpreferences 保存但在 if 语句上崩溃

转载 作者:行者123 更新时间:2023-11-29 18:03:47 25 4
gpt4 key购买 nike

因此对于我正在开发的应用程序,我想使用 sharedpreferences 来保存数据,在这种情况下我想保存用户选择的语言。这是通过一个数字完成的,它会保存这个但是当我尝试在我的菜单页面上使用这个值时它总是崩溃

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)

最佳答案

改变

button1.setText(taal);

原来如此

button1.setText(String.valueOf(taal));

通过传递一个整数 (taal),Android 会尝试查找资源 ID 但会失败。

关于android - sharedpreferences 保存但在 if 语句上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14425960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com