Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I suppose that in the line of where you actually save the "progress" into the SharedPreferences, <strong>you will accidentially be saving a String instead of an Integer</strong>. </p> <p>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.</p> <p><strong>UPDATE:</strong></p> <p>Try getting the SharedPreferences like this:</p> <pre><code>SharedPreferences sp = c.getSharedPreferences("MySharedPrefs", 0); </code></pre> <p>Also consider <strong>cleaning your project</strong>.</p> <p>I also cant understand <strong>why you restart your Activity</strong> everytime you call weightLoss(), could you explain?</p> <pre><code>Intent refresh = new Intent(this, Progress.class); startActivity(refresh); this.finish(); </code></pre> <p><strong>This is how you should refresh your UI-Components:</strong></p> <p>Simply create a refresh method that reads the values out of the SharedPreferences anytime you want and sets them to the TextViews.</p> <pre><code>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 &lt; 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 } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload