title: Getting a ClassCastException: java.lang.Integer cannot be cast to java.lang.String error without casting
body: <p>I am working on an app for Android and can't figure out the error or mistake which I am making. I am searching for the error for more than 4 hours.</p>
  <p>I am getting the following Exception working with SharedPreferences:</p>
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
     Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
       at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:235)
       at de.immozukunft.quicksteuer.QuickSteuerActivity.updateTextView(QuickSteuerActivity.java:56)
       at de.immozukunft.quicksteuer.QuickSteuerActivity.onCreate(QuickSteuerActivity.java:36)
  </code></pre>
  <p>But I am not doing a cast in that line according to my understanding. Line 36 is the call of the method <em>updateTextView()</em>.</p>
  <pre><code>private void updateTextView() {
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  
      int legalForm = Integer.parseInt(prefs.getString(&quot;legal_form&quot;, &quot;1&quot;));
      boolean industrialTax = prefs.getBoolean(&quot;industrial_tax&quot;, false);
      boolean turnoverTax = prefs.getBoolean(&quot;turnover_tax&quot;, false);
      boolean disbursal = prefs.getBoolean(&quot;disbursal&quot;, false);
  
      String tmp = prefs.getString(&quot;capitalownership&quot;, &quot;0&quot;); //this is line 56
  
      int capitalOwnership = Integer.parseInt(tmp);
      int estIncome = Integer.parseInt(prefs.getString(&quot;est_income&quot;, &quot;0&quot;));
  }
  </code></pre>
  <p>My preference in the XML-File is the following:</p>
  <pre><code>&lt;EditTextPreference
  android:defaultValue=&quot;0&quot;
  android:dialogTitle=&quot;@string/capitalownership&quot;
  android:inputType=&quot;number&quot;
  android:key=&quot;capitalownership&quot;
  android:title=&quot;@string/capitalownership&quot; /&gt;
  </code></pre>
  <p>I needed I will post the full code and full Error list.</p>
  <p>I am relatively new to Android.</p>
  <h2>Solution</h2>
  <pre><code>    private void updateTextView() {
      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  
      String legalForm = prefs.getString(&quot;legal_form&quot;, &quot;1&quot;);
      boolean industrialTax = prefs.getBoolean(&quot;industrial_tax&quot;, false);
      boolean turnoverTax = prefs.getBoolean(&quot;turnover_tax&quot;, false);
      boolean disbursal = prefs.getBoolean(&quot;disbursal&quot;, false);
      String capitalOwnership = prefs.getString(&quot;capitalownership&quot;, &quot;0&quot;);
      String estIncome = prefs.getString(&quot;est_income&quot;, &quot;1&quot;);
  
      settingstv.setText(getString(R.string.overview_settings,
              legalForm,
              Boolean.toString(industrialTax),
              Boolean.toString(turnoverTax),
              Boolean.toString(disbursal),
              capitalOwnership,
              estIncome));
  
      try {
          if (!compareCurrentWithStoredVersionCode(this, prefs, &quot;storedVersionCode&quot;)) {
              SharedPreferences.Editor editor = prefs.edit();
              editor.putString(&quot;legal_form&quot;, legalForm);
              editor.putBoolean(&quot;industrial_tax&quot;, industrialTax);
              editor.putBoolean(&quot;turnover_tax&quot;, turnoverTax);
              editor.putBoolean(&quot;disbursal&quot;, disbursal);
              editor.putString(&quot;capitalownership&quot;, capitalOwnership);
              editor.putString(&quot;est_income&quot;, estIncome);
              editor.commit();
          }
      } catch (PackageManager.NameNotFoundException e) {
          Log.e(TAG, &quot;updateTextView()&quot;, e);
      }
  }
  </code></pre>