1
我試圖在第一次啓動應用程序時生成一個隨機數,並將其顯示在文本視圖中。此外,我需要隨機文本保存並加載下一個應用程序的開始。保存並加載隨機數不起作用
所以這是代碼I'n嘗試:
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
請解釋**,但此代碼不起作用** 您的預期結果是什麼,您得到了什麼結果? – SripadRaj
@SripadRaj它在文本視圖中只顯示任何內容... –
@J Doe Add String valueInt = Integer.toString(myIntValue); iddd.setText(valueInt); – Stanojkovic