使用SharedPreferences的ClassCastException [英] ClassCastException using SharedPreferences

查看:84
本文介绍了使用SharedPreferences的ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译器将引发ClassCastException:无法将字符串强制转换为以下第96行的int值.我一生都无法弄清楚为什么,任何帮助都将真正付诸实践.

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 appericiated.

代码:

public class Progress extends Activity {

private TextView names, current, goal, start;
private EditText updateBox;
private ProgressBar progress;
private String currentWeight, goalS, startS;
private int currentWeightInt, weightUpdate;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_progress);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String name = preferences.getString("name", "You need to create a profile first!");
    names = (TextView) findViewById(R.id.noProf);
    names.setText("Welcome back " + name);
    start = (TextView) findViewById(R.id.start);
    startS = preferences.getString("start", "");
    start.setText("Start:" + startS);

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

    goal = (TextView) findViewById(R.id.goal);
    goalS = preferences.getString("goal", " ");
    goal.setText("goal: " + goalS);

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

}

public void update(View view) {
    updateBox = (EditText) findViewById(R.id.updateWeight);
    if (updateBox.getText().toString().matches(" ")) {
        Toast.makeText(this, "Please enter a weight", Toast.LENGTH_LONG).show();
    }
    else {
        weightUpdate = Integer.parseInt(updateBox.getText().toString());

        if (weightUpdate > currentWeightInt) {
            weightGain();
            Toast.makeText(this, "weight gain", Toast.LENGTH_LONG).show();
        }

        else if (weightUpdate < currentWeightInt) {
            Toast.makeText(this, "weight loss", Toast.LENGTH_LONG).show();
            weightLoss();

        }

        else {
            Toast.makeText(this, "stayed the same", Toast.LENGTH_LONG).show();

        }
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.progress, menu);
    return true;
}

private void weightGain() {
    if (weightUpdate > currentWeightInt) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("current", Integer.toString(weightUpdate));
        editor.commit();

    }

}

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();
        Intent refresh = new Intent(this, Progress.class);

        startActivity(refresh);

        this.finish();

    }
}

}

堆栈跟踪:

 08-17 18:44:25.565: D/qdmemalloc(11781): ion: Mapped buffer base:0x6c501000 size:8355840 offset:0 fd:56
08-17 18:44:25.565: D/qdmemalloc(11781): ion: Mapped buffer base:0x67680000 size:4096 offset:0 fd:57
08-17 18:44:28.418: D/qdmemalloc(11781): ion: Mapped buffer base:0x6cd09000 size:8355840 offset:0 fd:58
08-17 18:44:28.418: D/qdmemalloc(11781): ion: Mapped buffer base:0x67b33000 size:4096 offset:0 fd:59
08-17 18:44:29.459: I/InputMethodManager(11781): windowGainedFocus, mServedView=android.widget.EditText{4188f960 VFED..CL .F....ID 225,921-855,1039 #7f08001b app:id/updateWeight}, inputType=0x2, softInputMode=0x120, pid=11781
08-17 18:44:29.509: D/qdmemalloc(11781): ion: Mapped buffer base:0x6d668000 size:8355840 offset:0 fd:61
08-17 18:44:29.509: D/qdmemalloc(11781): ion: Mapped buffer base:0x67cca000 size:4096 offset:0 fd:62
08-17 18:44:29.529: D/qdmemalloc(11781): ion: Unmapping buffer  base:0x6bbe9000 size:8355840
08-17 18:44:29.529: D/qdmemalloc(11781): ion: Unmapping buffer  base:0x6720d000 size:4096
08-17 18:44:29.529: D/qdmemalloc(11781): ion: Unmapping buffer  base:0x6c501000 size:8355840
08-17 18:44:29.529: D/qdmemalloc(11781): ion: Unmapping buffer  base:0x67680000 size:4096
08-17 18:44:29.529: D/qdmemalloc(11781): ion: Unmapping buffer  base:0x6cd09000 size:8355840
08-17 18:44:29.529: D/qdmemalloc(11781): ion: Unmapping buffer  base:0x67b33000 size:4096
08-17 18:44:29.980: D/qdmemalloc(11781): ion: Mapped buffer base:0x6bbe9000 size:8355840 offset:0 fd:54
08-17 18:44:29.980: D/qdmemalloc(11781): ion: Mapped buffer base:0x6720d000 size:4096 offset:0 fd:56
08-17 18:44:30.270: D/qdmemalloc(11781): ion: Mapped buffer base:0x6c501000 size:8355840 offset:0 fd:57
08-17 18:44:30.270: D/qdmemalloc(11781): ion: Mapped buffer base:0x67680000 size:4096 offset:0 fd:58
08-17 18:44:35.976: W/dalvikvm(11781): threadid=1: thread exiting with uncaught exception (group=0x413feba0)
08-17 18:44:36.006: E/AndroidRuntime(11781): FATAL EXCEPTION: main
08-17 18:44:36.006: E/AndroidRuntime(11781): java.lang.IllegalStateException: Could not execute method of the activity
08-17 18:44:36.006: E/AndroidRuntime(11781):    at android.view.View$1.onClick(View.java:3626)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at android.view.View.performClick(View.java:4231)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at android.view.View$PerformClick.run(View.java:17537)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at android.os.Handler.handleCallback(Handler.java:725)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at android.os.Looper.loop(Looper.java:158)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at android.app.ActivityThread.main(ActivityThread.java:5777)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at java.lang.reflect.Method.invokeNative(Native Method)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at java.lang.reflect.Method.invoke(Method.java:511)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at dalvik.system.NativeStart.main(Native Method)
08-17 18:44:36.006: E/AndroidRuntime(11781): Caused by: java.lang.reflect.InvocationTargetException
08-17 18:44:36.006: E/AndroidRuntime(11781):    at java.lang.reflect.Method.invokeNative(Native Method)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at java.lang.reflect.Method.invoke(Method.java:511)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at android.view.View$1.onClick(View.java:3621)
08-17 18:44:36.006: E/AndroidRuntime(11781):    ... 11 more
08-17 18:44:36.006: E/AndroidRuntime(11781): Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
08-17 18:44:36.006: E/AndroidRuntime(11781):    at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:240)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at com.example.smallchangebigloss.Progress.weightLoss(Progress.java:96)
08-17 18:44:36.006: E/AndroidRuntime(11781):    at com.example.smallchangebigloss.Progress.update(Progress.java:63)
08-17 18:44:36.006: E/AndroidRuntime(11781):    ... 14 more

推荐答案

解决了这个问题,正如您正确指出的那样,所有代码都是正确的.问题是我以前已经将偏好设置中的progress的值从字符串更改为int.这意味着在第一次检索它时它仍然是一个字符串.要解决此问题,就像从手机上卸载应用程序并重新安装一样简单.现在遇到了一些新问题,但本部分代码却没有.谢谢大家的帮助.

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.

这篇关于使用SharedPreferences的ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆