Sharedpreferences do not work, default values

In my application there are a lot of flags, spinner, editor and button. When I select an item from the counter, and then enter a specific value and press a button, the specific text of the checkbox changes. Now I have added the general settings, and I have two problems.

The first problem is that when I try to use sharedpreferences, the application crashes (as soon as I run an action containing these sharedpreferences).

private String getItemQuantity(String key){ SharedPreferences itemQuantitySP = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE); String sp1 = "sp1"; return itemQuantitySP.getString(key, sp1); } private void saveItemQuantity(String key, String value){ SharedPreferences itemQuantitySP = getApplicationContext().getSharedPreferences("bifrostPrefs", android.content.Context.MODE_PRIVATE); SharedPreferences.Editor editor = itemQuantitySP.edit(); editor.putString(key, value); editor.commit(); } 

Saving general settings:

 if (Integer.parseInt(quantityEditText.getText().toString()) == 250) { cb4.setText("Elaborate Totem (" + item1 + "/250)"); saveItemQuantity("cb4", cb4.getText().toString()); cb4.setChecked(true); } ... //REST OF THE CODE 

Getting sharedpreferences:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bifrost); getItemQuantity("cb4"); ... //REST OF THE CODE 

The second problem is that I have a lot of checkboxes and setting the default value for each of them will be very time consuming. So, is there a way to just read the default value from XML? As you can see, at the moment I created a new variable sp1 and set it as the default, but of course I want to get rid of it.

Logcat File:

 06-27 16:31:44.782: E/AndroidRuntime(13139): FATAL EXCEPTION: main 06-27 16:31:44.782: E/AndroidRuntime(13139): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matthewstudios.gw2legendary/com.example.gw2legendary.Bifrost}: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.app.ActivityThread.access$600(ActivityThread.java:141) 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.os.Handler.dispatchMessage(Handler.java:99) 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.os.Looper.loop(Looper.java:137) 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.app.ActivityThread.main(ActivityThread.java:5041) 06-27 16:31:44.782: E/AndroidRuntime(13139): at java.lang.reflect.Method.invokeNative(Native Method) 06-27 16:31:44.782: E/AndroidRuntime(13139): at java.lang.reflect.Method.invoke(Method.java:511) 06-27 16:31:44.782: E/AndroidRuntime(13139): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-27 16:31:44.782: E/AndroidRuntime(13139): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-27 16:31:44.782: E/AndroidRuntime(13139): at dalvik.system.NativeStart.main(Native Method) 06-27 16:31:44.782: E/AndroidRuntime(13139): Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224) 06-27 16:31:44.782: E/AndroidRuntime(13139): at com.example.gw2legendary.Bifrost.getItemQuantity(Bifrost.java:849) 06-27 16:31:44.782: E/AndroidRuntime(13139): at com.example.gw2legendary.Bifrost.onCreate(Bifrost.java:152) 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.app.Activity.performCreate(Activity.java:5104) 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 06-27 16:31:44.782: E/AndroidRuntime(13139): ... 11 more 06-27 16:31:49.391: E/fb4a(:<default>):MmsConfig(13225): MmsConfig.loadMmsSettings mms_config.xml missing uaProfUrl setting 

Code Part:

 public void submitQuantityButton (View v){ final Spinner sItems = (Spinner)findViewById(R.id.spinner1); final Context context = this; final CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4); final CheckBox cb5 = (CheckBox) findViewById(R.id.checkBox5); final CheckBox cb6 = (CheckBox) findViewById(R.id.checkBox6); final CheckBox cb7 = (CheckBox) findViewById(R.id.checkBox7); final CheckBox cb9 = (CheckBox) findViewById(R.id.checkBox9); final CheckBox cb10 = (CheckBox) findViewById(R.id.checkBox10); final CheckBox cb11 = (CheckBox) findViewById(R.id.checkBox11); final CheckBox cb12 = (CheckBox) findViewById(R.id.checkBox12); final CheckBox cb13 = (CheckBox) findViewById(R.id.checkBox13); final CheckBox cb15 = (CheckBox) findViewById(R.id.checkBox15); final CheckBox cb16 = (CheckBox) findViewById(R.id.checkBox16); final CheckBox cb17 = (CheckBox) findViewById(R.id.checkBox17); final CheckBox cb18 = (CheckBox) findViewById(R.id.checkBox18); final CheckBox cb25 = (CheckBox) findViewById(R.id.checkBox25); final CheckBox cb27 = (CheckBox) findViewById(R.id.checkBox27); final CheckBox cb28 = (CheckBox) findViewById(R.id.checkBox28); final CheckBox cb29 = (CheckBox) findViewById(R.id.checkBox29); final CheckBox cb30 = (CheckBox) findViewById(R.id.checkBox30); final CheckBox cb31 = (CheckBox) findViewById(R.id.checkBox31); final CheckBox cb33 = (CheckBox) findViewById(R.id.checkBox33); final CheckBox cb34 = (CheckBox) findViewById(R.id.checkBox34); final CheckBox cb35 = (CheckBox) findViewById(R.id.checkBox35); final CheckBox cb36 = (CheckBox) findViewById(R.id.checkBox36); final CheckBox cb37 = (CheckBox) findViewById(R.id.checkBox37); //WRONG VALUE ERROR final AlertDialog.Builder wrongValueBuilder = new AlertDialog.Builder(context); wrongValueBuilder.setTitle("Warning"); wrongValueBuilder.setMessage("The value you entered is too high!"); wrongValueBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); //EMPTY TEXT ERROR final AlertDialog.Builder emptyETextErrorBuilder = new AlertDialog.Builder(context); emptyETextErrorBuilder.setTitle("Warning"); emptyETextErrorBuilder.setMessage("Please enter a value before pressing this button"); emptyETextErrorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); final int position = sItems.getSelectedItemPosition(); EditText quantityEditText = (EditText)findViewById(R.id.editText1); switch (position){ case 0: AlertDialog.Builder spinnerErrorBuilder = new AlertDialog.Builder(context); spinnerErrorBuilder.setTitle("Warning"); spinnerErrorBuilder.setMessage("Please choose an item from the list above and then enter a certain value"); spinnerErrorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog spinnerError = spinnerErrorBuilder.create(); spinnerError.show(); break; case 1: String item1 = quantityEditText.getText().toString(); if (item1.matches("")) { AlertDialog emptyETextError = emptyETextErrorBuilder.create(); emptyETextError.show(); } else { if (Integer.parseInt(quantityEditText.getText().toString()) <= 250) { if (Integer.parseInt(quantityEditText.getText().toString()) == 250) { cb4.setText("Elaborate Totem (" + item1 + "/250)"); saveItemQuantity("cb4", cb4.getText().toString()); cb4.setChecked(true); } else { cb4.setText("Elaborate Totem (" + item1 + "/250)"); saveItemQuantity("cb4", cb4.getText().toString()); cb4.setChecked(false); } } 
+4
source share
2 answers

The column and the next three lines show where the problem is:

 06-27 16:31:44.782: E/AndroidRuntime(13139): Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String 06-27 16:31:44.782: E/AndroidRuntime(13139): at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224) 06-27 16:31:44.782: E/AndroidRuntime(13139): at com.example.gw2legendary.Bifrost.getItemQuantity(Bifrost.java:849) 

So, you have a problem in the Bifrost.java file, on line 849, in the getItemQuantity() method. And this corresponds to the call to the getString() method. It is very interesting to look at 3 lines and try to understand how you can get all this information, because you will have to do the same every time you have an exception in your code.

I can assume that the value you are trying to get from SharedPreferences was saved as Boolean and you are trying to get it as String , so you have this ClassCastException exception. Just reset the value that you saved in SharedPreferences with this key.

For more information, try to learn how to debug an application and use breakpoints to check the values ​​of variables at runtime. Breakpoints let you stop execution exactly where you want, and look for the state and values ​​of all variables. Debugging with an IDE, such as Eclipse or NetBeans, is very simple and can help you in many situations.

+4
source

Obviously, the error lies in reading stacktrace:

java.lang.ClassCastException: java.lang.Boolean cannot be passed to java.lang.String

And yes, you can read the default values ​​from the XML file, read arrays.xml and http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray here

0
source

Source: https://habr.com/ru/post/1488490/


All Articles