gpt4 book ai didi

java - 保存和加载随机数不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:15:35 26 4
gpt4 key购买 nike

我试图在应用程序第一次启动时生成一个随机数,并在 TextView 中显示它。我还需要保存并加载随机文本以供下次启动应用程序。

这是我正在尝试的代码:

SharedPreferences prefs = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);

onResume(); {
super.onResume();

if (prefs.getBoolean("firstrun", true)) {

Random r = new Random();
int i1 = r.nextInt(80 - 65) + 65;

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("random id", i1);
editor.commit();


prefs.edit().putBoolean("firstrun", false).commit();

}
}

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
int myIntValue = sp.getInt("random id", -1);


TextView iddd = (TextView) findViewById(R.id.id);
iddd.setText(myIntValue);
}

但是这段代码不起作用...
问题是什么?我做错了什么?

日志:

09-12 12:27:27.203: E/test(10532): Exception
09-12 12:27:27.205: E/AndroidRuntime(10532): FATAL EXCEPTION: main
09-12 12:27:27.205: E/AndroidRuntime(10532): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hhh/com.example.hhh.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x43
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.app.ActivityThread.access$600(ActivityThread.java:156)
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.os.Looper.loop(Looper.java:153)
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.app.ActivityThread.main(ActivityThread.java:5299)
09-12 12:27:27.205: E/AndroidRuntime(10532): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 12:27:27.205: E/AndroidRuntime(10532): at java.lang.reflect.Method.invoke(Method.java:511)
09-12 12:27:27.205: E/AndroidRuntime(10532): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
09-12 12:27:27.205: E/AndroidRuntime(10532): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-12 12:27:27.205: E/AndroidRuntime(10532): at dalvik.system.NativeStart.main(Native Method)
09-12 12:27:27.205: E/AndroidRuntime(10532): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x43
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.content.res.Resources.getText(Resources.java:242)
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.widget.TextView.setText(TextView.java:3805)
09-12 12:27:27.205: E/AndroidRuntime(10532): at com.example.hhh.MainActivity.onCreate(MainActivity.java:78)
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.app.Activity.performCreate(Activity.java:5182)
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
09-12 12:27:27.205: E/AndroidRuntime(10532): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
09-12 12:27:27.205: E/AndroidRuntime(10532): ... 11 more

最佳答案

android.content.res.Resources$NotFoundException: String resource ID #0x43

iddd.setText(myIntValue);

使用例如

iddd.setText(String.valueOf(myIntValue));

setText(int) 期望看到资源字符串 id; setText(CharSequence) 是您要调用的。

关于java - 保存和加载随机数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39445461/

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