Skip to content Skip to sidebar Skip to footer

ClassCastException Using SharedPreferences

The compiler is throwing ClassCastException: String cannot cast be to an int on line 96 of the below. I cannot for the life of me figure out why, and any help would be really apper

Solution 1:

Fixed this one- as rightly pointed out by you all the code was correct. The issue was that I had previously changed the value of progress in preferences from a string to an int. This meant when retrieving it the first time it was still a string. To rectify this was as simple as uninstalling the application form my phone and reinstalling it. Got some new issues now but not with this section of code. Thanks for your help everyone.


Solution 2:

I suppose that in the line of where you actually save the "progress" into the SharedPreferences, you will accidentially be saving a String instead of an Integer.

Then, upon trying to get the saved value you run into a ClassCastException because the saved value is actually a String and not an Integer.

UPDATE:

Try getting the SharedPreferences like this:

SharedPreferences sp = c.getSharedPreferences("MySharedPrefs", 0);

Also consider cleaning your project.

I also cant understand why you restart your Activity everytime you call weightLoss(), could you explain?

Intent refresh = new Intent(this, Progress.class);

startActivity(refresh);
this.finish();

This is how you should refresh your UI-Components:

Simply create a refresh method that reads the values out of the SharedPreferences anytime you want and sets them to the TextViews.

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_progress);

    names = (TextView) findViewById(R.id.noProf);
    start = (TextView) findViewById(R.id.start);
    current = (TextView) findViewById(R.id.currently);
    goal = (TextView) findViewById(R.id.goal);

    progress = (ProgressBar) findViewById(R.id.progressBar1);

    refresh(); // call this method everytime you want to update the textviews and so on...
}

// CALL THIS METHOD EVERYTIME YOU WANT TO REFRESH INSTEAD OF RESTARTING YOUR ACTIVITY
public void refresh() {

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String name = preferences.getString("name", "You need to create a profile first!");

    names.setText("Welcome back " + name);
    startS = preferences.getString("start", "");

    start.setText("Start:" + startS);

    currentWeight = preferences.getString("current", "");
    currentWeightInt = Integer.parseInt(currentWeight);
    current.setText("currently  " + currentWeight);

    goalS = preferences.getString("goal", " ");

    goal.setText("goal: " + goalS);
}

// your other methods...

private void weightLoss() {

    if (weightUpdate < currentWeightInt) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        int toSet = preferences.getInt("progress",0);
        progress.setProgress(toSet);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("current", Integer.toString(weightUpdate));
        editor.commit();
        double first = (Double.parseDouble(startS) - Double
            .parseDouble(currentWeight));
        double second = (Double.parseDouble(startS) - Double
            .parseDouble(goalS));
        double last = first / second;
        double result = last * 100;
        int resultInt = (int) result;
        progress.setProgress(resultInt);

        editor.putInt("progress", resultInt);
        editor.commit();

        refresh(); // DO THIS HERE INSTEAD OF RESTARTING THE ACTIVITY
    }
}

Post a Comment for "ClassCastException Using SharedPreferences"