Pavan Putra Boy Pavan Putra Boy - 2 months ago 9
Android Question

Getting android.content.res.Resources NotFoundException while saving in shared Preferences

I want to save text from

EditText
to
sharedPrefs
file named
prefs
in
mode_private
and later on show it in a
textview
.

code :

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);


I have put this code in button
onClick
!

Now the data which I want to save is of integer type .Now the problem is that there is no error shown in code but when I run code and enter data and click on a button app is crashing causing this error!

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


Now what should I do to remove it ?

Answer

Change

tv.setText(c);

to

tv.setText(str);

to use setText(CharSequence) which expects a string instead of setText(int) which expects a resource id.