title: Android ClassCastException after reading from SharedPreferences
body: <p>Yesterday, I built a SettingsActivity for my Android app where the user can enter the URL of a webservice-server. I save this using:</p>
  
  <pre><code>editor = sharedPref.edit();
  editor.putString(
      getString(R.string.SAVED_URL_MOBILEHELPER), lvsURL_mobilehelper);
  editor.commit();
  </code></pre>
  
  <p>in the SharedPreferences after the "save" button is pressed.</p>
  
  <p>In the <code>onStart()</code> I read the setting to set the saved value into the belonging text-field:</p>
  
  <pre><code>// line 29
  String lvsURL_mobilehelper = sharedPref.getString(
      getString(R.string.SAVED_URL_MOBILEHELPER), "");
  </code></pre>
  
  <p>This also worked well yesterday; the last string I entered and successfully read from the settings was "testURL12345".</p>
  
  <p>Today, I was trying to add user-authentication around my application and since that, I get a <code>ClassCastException</code> when I open the SettingsActivity:</p>
  
  <pre><code>Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to
      java.lang.String
  at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224)
  at de.unibonn.sdb.wissappmobile.activities.SettingsActivity.onResume(
      SettingsActivity.java:29)
  </code></pre>
  
  <p>Does anyone have an idea why this all worked fine yesterday and now it doesn't?</p>
  
  <p>Note: I don't want to store the user credentials in an <code>AccountManager</code> or persistent in my preferences, because the app shall be used on a "business tablet" and not a "personal tablet". The user credentials are needed for HTTP-Basic authentication. So my idea is to check in the parent Activity if the user is "logged in" and not inactive for more than 1800 seconds.  </p>
  
  <h2>SettingsActivity:</h2>
  
  <pre><code>/*
   * Activity for settings page (webservice URL)
   */
  public class SettingsActivity extends AppFragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_settings);
    }
  
    @Override
    protected void onResume() {
      super.onResume();
  
      // Fill content
  
      // URL of the Mobile Helper
      String lvsURL_mobilehelper = sharedPref.getString(
          getString(R.string.SAVED_URL_MOBILEHELPER), "");
      EditText dfsURLMobileHelper = (EditText) findViewById(R.id.dfsURLMobileHelper);
      dfsURLMobileHelper.setText(lvsURL_mobilehelper);        
    }
  
    /*
     * Action called when pressing the "Save"-Button
     * 
     * Saves the entered Data in a local file.
     */
    public void ClickBtnSave(View view) {
      EditText dfsURLMobileHelper = (EditText) findViewById(R.id.dfsURLMobileHelper);
      TextView txtError = (TextView) findViewById(R.id.txtError);
  
      String lvsURL_mobilehelper = dfsURLMobileHelper.getText().toString();
      String Eceptiontext = "";
  
      Boolean success = false;
  
      // Write to file
      try {
        editor = sharedPref.edit();
        editor.putString(getString(R.string.SAVED_URL_MOBILEHELPER), lvsURL_mobilehelper);
        editor.commit();
  
        success = true;
      } catch (Exception e) {
        success = false;
        Eceptiontext = e.getLocalizedMessage();
      }
  
      if (success) {
        txtError.setText(getString(R.string.SAVING_SUCCESS));
      } else {
        txtError.setText(getString(R.string.SAVING_FAILED) + " : " +  Eceptiontext);
      }
    }     
  
    /*
     * Action called when pressing the "Back"-Button
     * 
     * Opens the Search-Acitivity
     */
    public void ClickBtnBack(View view) {
      // Back to SearchActivity
      Intent intent = new Intent(this, SearchActivity.class);
      startActivity(intent);
    }        
  }
  </code></pre>
  
  <h2>Parent AppFragmentActivity:</h2>
  
  <pre><code>/**
   * Class to handle global Callbacks (e.g. user credentials)
   */
  public class AppFragmentActivity extends FragmentActivity  {
  
    protected SharedPreferences sharedPref;
    protected SharedPreferences.Editor editor;    
  
    protected String WebServiceUsername;
    protected String WebServicePassword;
  
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_appfragmentactivity);
    } 
  
  
    @Override
    protected void onResume () {
      super.onResume();
  
  
      // Check if user is "logged in".
      // Meaning: Are there given user credentials and are they valid of was the user inactive for too long?
  
      // We only do this "onResume" because this callback is the only one, which is called everytime an user 
      // starts/restarts/resumes an application
      checkForUserCredentials();
  
  
      // Set new "last action" now "now"
      setLastAction(new Date().getTime());
    }
  
    @Override
    protected void onStart () {
      // Fill content
      super.onStart();
  
      // Set global sharedPreferences
      sharedPref = getSharedPreferences(
          getString(R.string.FILE_settings_file), Context.MODE_PRIVATE);
    }
  
  
  
    /*
     * Checks if user credentials are valid meaning if they are set and not too old
     */
    private void checkForUserCredentials() {
      // Get filehandle to PreferencesFile
  
  
      long TimeLastAction = sharedPref.getLong(
          getString(R.string.SETTINGS_USER_LAST_ACTION), 0);
      long TimeNow = new Date().getTime();
  
      // Ask for User credentials when last action is too long ago
      if(TimeLastAction &lt; (TimeNow - 1800)) {
        // Inactive for too long
  
        // Set credentials back
        setUsernameAndPassword("", "");
  
      } else {
        WebServiceUsername = sharedPref.getString(
            getString(R.string.SETTINGS_USER_USERNAME), "");
        WebServicePassword = sharedPref.getString(
            getString(R.string.SETTINGS_USER_PASSWORD), "");              
      }
    }
  
    /*
     * Saves the given last action in the sharedPreferences
     * @param long LastAction - Time of the last action
     */
    private void setLastAction(long LastAction) {
      editor = sharedPref.edit();
      editor.putLong(getString(R.string.SETTINGS_USER_LAST_ACTION), LastAction);
      editor.commit();        
    }
  
    /*
     * Saves the given username and userpassword sharedPreferences
     * @param String username
     * @param String password
     */
    private void setUsernameAndPassword(String username, String password) {
      editor = sharedPref.edit();
      editor.putString(
          getString(R.string.SETTINGS_USER_USERNAME), username);
      editor.putString(
          getString(R.string.SETTINGS_USER_PASSWORD), username);
      editor.commit();    
  
      WebServiceUsername = username;
      WebServicePassword = password;
    } 
  
    /*
     * Method called when pressing the OK-Button
     */
      public void ClickBtnOK(View view) {
          // Save User-Creentials
  
        EditText dfsUsername = (EditText) findViewById(R.id.dfsUsername);
        String lvsUsername = dfsUsername.getText().toString();        
  
        EditText dfsPassword = (EditText) findViewById(R.id.dfsPassword);
        String lvsPassword = dfsPassword.getText().toString();        
  
        if(lvsUsername.equals("") || lvsPassword.equals("")) {
          TextView txtError = (TextView) findViewById(R.id.txtError);
          txtError.setText(getString(R.string.ERR_Name_or_Password_empty));
        } else {
          // Save credentials
          setUsernameAndPassword(lvsUsername, lvsPassword);
          setLastAction(new Date().getTime());
  
          // open Searchactivity
            Intent intent = new Intent(this, SearchActivity.class);
            startActivity(intent);            
  
        }
      }   
  
      @Override
      protected void onPause() {
        super.onPause();
        setLastAction(new Date().getTime());
      }
  
      @Override
    protected void onStop() {
      super.onStop();
      setLastAction(0);
      setUsernameAndPassword("", "");
    }
  }
  </code></pre>