title: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
body: <p>I trying to save resource id of  an image in sharedpreferences on button click and then trying to set that resid to the image. But i am getting "String cannot cast to Integer" error. My question can be silly but I want to solve it. I will be much thankful for that .My code is here.</p>
  
  <pre><code>b4.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              int res=R.drawable.kiss;
              image.setImageResource(res);
              editor.putInt(getString(R.string.keyimage1),res);
              editor.apply();
          }
      });
  </code></pre>
  
  <p>default value if key not exist<code>
  int defaultimageres=R.mipmap.ic_launcher;</code></p>
  
  <p>Error I am getting in line below.</p>
  
  <pre><code>int storedImageres=sharedPreferences.getInt(getString(R.string.keyimage1),defaultimageres);
  </code></pre>
  
  <p>and here I am setting the resId to image </p>
  
  <pre><code>image.setImageResource(storedImageres);
  </code></pre>
  
  <p>LogCat:</p>
  
  <pre><code>FATAL EXCEPTION: main
  
                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pra1.savingdatasample/com.example.pra1.savingdatasample.MainActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                    at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:158)
                    at android.app.ActivityThread.main(ActivityThread.java:7225)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                 Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
                    at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:242)
                    at com.example.pra1.savingdatasample.MainActivity.onCreate(MainActivity.java:47)
                    at android.app.Activity.performCreate(Activity.java:6876)
                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
                    at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                    at android.os.Looper.loop(Looper.java:158) 
                    at android.app.ActivityThread.main(ActivityThread.java:7225) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
  </code></pre>
  
  <p>here is my onCreate method: I know its complete mess :P</p>
  
  <pre><code> protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      b1=(Button)findViewById(R.id.button);
      b2=(Button)findViewById(R.id.button2);
      b3=(Button)findViewById(R.id.button3);
      b4=(Button)findViewById(R.id.button4);
      ed1=(EditText)findViewById(R.id.editText1);
      ed2=(EditText)findViewById(R.id.editText2);
      tv1=(TextView)findViewById(R.id.textView);
      tv2=(TextView)findViewById(R.id.textView2);
      image=(ImageView)findViewById(R.id.imageView);
  
      SharedPreferences sharedPreferences=this.getPreferences(Context.MODE_PRIVATE);
      final SharedPreferences.Editor editor=sharedPreferences.edit();
      int defaultimageres=R.mipmap.ic_launcher;
      String defaultvaluetext1=getResources().getString(R.string.text1);
      String storedtext1=sharedPreferences.getString(getString(R.string.keytextview1),defaultvaluetext1);
      int storedImageres=sharedPreferences.getInt(getString(R.string.keyimage1),defaultimageres);
      image.setImageResource(storedImageres);
      tv1.setText(storedtext1);
  
      String defaultvaluetext2=getResources().getString(R.string.text2);
      String storedtext2=sharedPreferences.getString(getString(R.string.keytextview2),defaultvaluetext2);
      tv2.setText(storedtext2);
      b1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            String text1=ed1.getText().toString();
              if(text1.equals(null)||text1.equals("")){
                 Toast.makeText(getApplicationContext(),"empty text",Toast.LENGTH_SHORT).show();
              }else {
                  tv1.setText(text1);
                  editor.putString(getString(R.string.keytextview1), text1);
                  editor.apply();
                  ed1.setText("");
                  InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                  manager.hideSoftInputFromWindow(ed1.getWindowToken(), 0);
              }
          }
      });
      b2.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            String text2= ed2.getText().toString();
              if(text2.equals(null)||text2.equals("")){
                  Toast.makeText(getApplicationContext(),"empty text",Toast.LENGTH_SHORT).show();
              }
              else {
                  tv2.setText(text2);
                  editor.putString(getString(R.string.keytextview2), text2);
                  editor.apply();
                  ed2.setText("");
                  InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                  manager.hideSoftInputFromWindow(ed2.getWindowToken(), 0);
              }
          }
      });
      b3.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              int res=R.drawable.rtl;
              image.setImageResource(res);
              editor.putInt(getString(R.string.keyimage1),res);
              editor.commit();
          }
      });
      b4.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              int res=R.drawable.kiss;
              image.setImageResource(res);
              editor.putInt(getString(R.string.keyimage1),res);
              editor.commit();
          }
      });
  }
  </code></pre>