在使用SharedPreferences期间获取java.lang.ClassCastException

时间:2018-04-02 10:36:31

标签: android sharedpreferences classcastexception

我在我的应用中使用SharedPreferences来保存小Integer数据。但我在这行中得到ClassCastException

int number = mySharedPref.getInt("numberOne",0);

这是我的代码。

存储数据。

mySharedPref=getSharedPreferences("MyPref",Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = mySharedPref.edit();
            editor.putInt("numberOne",myInteger);

获取数据

 mySharedPref = getSharedPreferences("MyPref",Context.MODE_PRIVATE);
    int number= mySharedPref.getInt("numberOne",0);
    display(number);

3 个答案:

答案 0 :(得分:2)

我认为问题在于您尚未提交存储在 package view; import javafx.scene.text.Text; public class Overview extends VBox{ private TextField info; public Overview() { //Adds a little border around the app so the text field doesn't go to the edges this.setPadding(new Insets(30, 30, 30, 30)); //----- Initialising text field -------- info = new TextField("Name:\n" + "PNumber:\n"+ "Email:\n"+ "Date:\n"+ "Course:\n"+ ""+ "Selected Modules:\n"+ "=============="); info.setAlignment(Pos.TOP_LEFT); info.setMinHeight(850); info.setEditable(false); //------ Adding the text to the Text Field -------- //-------- Putting it into VBox so that it stretches with the screen ---------- VBox fit = new VBox(info); this.getChildren().add(fit); } } 中的数据。 所以你应该做一个

SharedPreferences
存储数据时

答案 1 :(得分:1)

试试这个

存储数据

public static void setInteger(Context context, String key, Integer Value) {

        sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor sEdit = sharedPreferences.edit();
        sEdit.putInt(key, Value);
        sEdit.commit();

    }

获取数据

public static Integer getInteger(Context context, String key) {
    sharedPreferences = PreferenceManager
          .getDefaultSharedPreferences(context);
       Integer value = sharedPreferences.getInt(key, 0);

      return value;

}

答案 2 :(得分:0)

您必须调用SharedPreferences方法来保存private void showData() { SharedPreferences sharedPreferences = getSharedPreferences("MyFile", Context.MODE_PRIVATE); int number = sharedPreferences.getInt("numberOne", 0); display(number); } 中的数据更改。 这是获取数据方法

private void saveData(int value) {
    SharedPreferences sharedPreferences = getSharedPreferences("MyFile", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("numberOne", value);
    editor.commit();
}

这是get savemethod

String data=new String(content.getBytes(),"UTF-8");
         String x=data.replaceAll("[\\n\\r\\t]+", "");
         System.out.println(x+"=================================================");
        FormatedData="id:"+id+"\n"+"name:"+name+"\n"+"comment:"+comment;
         String id,name,comment;

         JsonParser parser = new JsonParser();

              System.out.println(x+"===================================================");
                JsonObject obj = (JsonObject) parser.parse(x).getAsJsonObject();
                System.out.println(obj+"===============================================");
                  name = obj.get("organizationNameEN").getAsString();
                  id = obj.get("id").getAsString();
                  comment=obj.get("comment").getAsString();

         String FormatedData="id:"+id+"\n"+"name:"+name+"\n"+"comment:"+comment;