Java 使用SharedReferences的ClassCastException

Java 使用SharedReferences的ClassCastException,java,android,string,int,classcastexception,Java,Android,String,Int,Classcastexception,编译器正在抛出ClassCastException:字符串不能强制转换为下面第96行的int。我一辈子都搞不懂为什么,任何帮助都会让我大吃一惊 代码: 我假设在您实际将“进度”保存到SharedReferences的行中,您将意外地保存一个字符串而不是一个整数 然后,在尝试获取保存的值时,会遇到ClassCastException,因为保存的值实际上是字符串而不是整数 更新: 尝试按如下方式获取SharedReference: SharedPreferences sp = c.getShared

编译器正在抛出ClassCastException:字符串不能强制转换为下面第96行的int。我一辈子都搞不懂为什么,任何帮助都会让我大吃一惊

代码:


我假设在您实际将“进度”保存到SharedReferences的行中,您将意外地保存一个字符串而不是一个整数

然后,在尝试获取保存的值时,会遇到ClassCastException,因为保存的值实际上是字符串而不是整数

更新:

尝试按如下方式获取SharedReference:

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

也要考虑<强>清理您的项目<强> < /P> 我也不明白你为什么每次叫减肥()都要重新开始运动,你能解释一下吗

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

startActivity(refresh);
this.finish();
这是刷新UI组件的方式:

只需创建一个刷新方法,随时从SharedReferences中读取值,并将其设置为TextView

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
    }
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u progress);
name=(TextView)findViewById(R.id.noProf);
start=(TextView)findViewById(R.id.start);
current=(TextView)findViewById(R.id.current);
目标=(TextView)findViewById(R.id.goal);
进度=(ProgressBar)findViewById(R.id.progressBar1);
refresh();//每次要更新TextView等时调用此方法。。。
}
//每次要刷新而不是重新启动活动时调用此方法
公共无效刷新(){
SharedReferences preferences=PreferenceManager.GetDefaultSharedReferences(此);
String name=preferences.getString(“name”,“您需要先创建配置文件!”);
name.setText(“欢迎回来”+name);
startS=preferences.getString(“start”,”);
start.setText(“start:+start”);
currentWeight=preferences.getString(“当前”和“”);
currentWeightInt=整数.parseInt(currentWeight);
current.setText(“当前”+当前权重);
goalS=preferences.getString(“goal”,“”);
goal.setText(“目标:+目标”);
}
//你的其他方法。。。
私人空间失重(){
如果(权重更新
修复了这个问题-正如您正确指出的,所有代码都是正确的。问题是,我以前将preferences中progress的值从字符串更改为int。这意味着在第一次检索时它仍然是字符串。要纠正这一点,只需卸载“我的手机”申请表并重新安装即可。现在有一些新问题,但这部分代码没有。谢谢大家的帮助。

看来你的进步了。setProgress(toSet);接受一个字符串,但传递一个intProgress是一个ProgressBar,并接受一个int.share完整代码以获得更好的帮助。还共享堆栈跟踪“下面的第4行”是(现在)
private EditText updateBox,这似乎根本不可能引发任何异常。使用注释标记引发异常的位置。添加了完整代码和堆栈跟踪。Progress是ProgressBar并使用int。您能看到我意外保存字符串的位置吗?我如何纠正这一点?谢谢。我刚刚检查了你的代码,“editor.putInt(“progress”,resultInt);”行保存了一个整数,这是完全正确的。请在您刚才在问题中发布的代码块周围发布其他代码。我会刷新它,以便在屏幕上更新值。清理不起作用,现在将尝试你的其他建议。更新是什么意思?你说的是文本视图吗?因为再次调用onCreate(),所以它们包含新的值?请看我更新的答案,我将向您展示如何刷新那里的组件。
Intent refresh = new Intent(this, Progress.class);

startActivity(refresh);
this.finish();
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
    }
}