保存共享首选项时获取android.content.res.Resources NotFoundException

时间:2016-09-28 07:24:44

标签: java android eclipse sharedpreferences

我想将EditText中的sharedPrefs文件保存到prefs mode_private textview,然后将其显示在String sal=ed.getText().toString(); int saly=Integer.parseInt(sal); String prefs="myPrefs"; String Salaryflag="mySalary"; SharedPreferences sp=getSharedPreferences(prefs,Context.MODE_PRIVATE); Editor ed=sp.edit(); ed.putInt(Salaryflag, saly); ed.commit(); Toast.makeText(getBaseContext(), "Salary Saved", Toast.LENGTH_SHORT).show(); int c=sp.getInt(Salaryflag, 0); String str=Integer.toString(c); tv.setText(c); 中。

代码

onClick

我已将此代码放在按钮09-28 03:11:38.124: E/AndroidRuntime(1352): FATAL EXCEPTION: main 09-28 03:11:38.124: E/AndroidRuntime(1352): Process: com.example.mybalancesheet, PID: 1352 09-28 03:11:38.124: E/AndroidRuntime(1352): java.lang.IllegalStateException: Could not execute method for android:onClick 09-28 03:11:38.124: E/AndroidRuntime(1352): at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 09-28 03:11:38.124: E/AndroidRuntime(1352): at android.view.View.performClick(View.java:4438) 09-28 03:11:38.124: E/AndroidRuntime(1352): at android.view.View$PerformClick.run(View.java:18422) 09-28 03:11:38.124: E/AndroidRuntime(1352): at android.os.Handler.handleCallback(Handler.java:733) 09-28 03:11:38.124: E/AndroidRuntime(1352): at android.os.Handler.dispatchMessage(Handler.java:95) 09-28 03:11:38.124: E/AndroidRuntime(1352): at android.os.Looper.loop(Looper.java:136) 09-28 03:11:38.124: E/AndroidRuntime(1352): at android.app.ActivityThread.main(ActivityThread.java:5001) 09-28 03:11:38.124: E/AndroidRuntime(1352): at java.lang.reflect.Method.invokeNative(Native Method) 09-28 03:11:38.124: E/AndroidRuntime(1352): at java.lang.reflect.Method.invoke(Method.java:515) 09-28 03:11:38.124: E/AndroidRuntime(1352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 09-28 03:11:38.124: E/AndroidRuntime(1352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 09-28 03:11:38.124: E/AndroidRuntime(1352): at dalvik.system.NativeStart.main(Native Method) 09-28 03:11:38.124: E/AndroidRuntime(1352): Caused by: java.lang.reflect.InvocationTargetException 09-28 03:11:38.124: E/AndroidRuntime(1352): at java.lang.reflect.Method.invokeNative(Native Method) 09-28 03:11:38.124: E/AndroidRuntime(1352): at java.lang.reflect.Method.invoke(Method.java:515) 09-28 03:11:38.124: E/AndroidRuntime(1352): at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 09-28 03:11:38.124: E/AndroidRuntime(1352): ... 11 more 09-28 03:11:38.124: E/AndroidRuntime(1352): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x3a98 09-28 03:11:38.124: E/AndroidRuntime(1352): at android.content.res.Resources.getText(Resources.java:244) 09-28 03:11:38.124: E/AndroidRuntime(1352): at android.widget.TextView.setText(TextView.java:3888) 09-28 03:11:38.124: E/AndroidRuntime(1352): at com.example.mybalancesheet.MainActivity.salaryEntered(MainActivity.java:60) 09-28 03:11:38.124: E/AndroidRuntime(1352): ... 14 more

现在我要保存的数据是整数类型。现在问题是代码中没有显示错误,但是当我运行代码并输入数据并单击按钮应用程序时崩溃导致此错误!

MediaPlayer

现在该怎么做才能删除它?

1 个答案:

答案 0 :(得分:3)

更改

tv.setText(c);

tv.setText(str);

使用期望字符串而不是setText(CharSequence)的{​​{1}}期望资源ID。