简体   繁体   中英

Prefs Error - Android ' Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String'

I was trying to implement prefs and solve a problem that I had previously, yet instead it just caused another problem -- a prefs issue. It seems to have shown it was from the lines I put on the onCreate() method.

Specifically in the log it said the following --

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.your.app/com.your.app.activity}: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:148)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                              Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
                                                 at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:225)
                                                 at com.your.app.activity.onCreate(ShopActivity.java:105*At the oncreate shown*)

So I was trying to fix this issue, but I could not find a solution. I was making a game with the code, and managed to create a system of coins, which you can see below, also involving the scrolling of cars using the tag system. The issue is that it seems to show up that error every time I load up that activity. I did not have this issue earlier and was using a different method. Here is the code. Thanks for all help. The code was also edited while trying to fix the error. --

For the onCreate method --

  prefs = this.getSharedPreferences(
            "com.this.app", Context.MODE_PRIVATE);
status1 = prefs.getString(savelocked,"Locked");
    status2 = prefs.getString(savelocked2,"Locked");
    status3 = prefs.getString(savelocked3,"Locked");
    status4 = prefs.getString(savelocked4,"Locked");
    status5 = prefs.getString(savelocked5,"Locked");

    locked = status1;
    locked2 = status2;
    locked3 = status3;
    locked4 = status4;
    locked5 = status5;

For the OnDestroy method --

 SharedPreferences.Editor prefsEditor = prefs.edit();


    prefsEditor.putString(savelocked,status1);
    prefsEditor.putString(savelocked2,status2);
    prefsEditor.putString(savelocked3,status3);
    prefsEditor.putString(savelocked4,status4);
    prefsEditor.putString(savelocked5,status5);
    prefsEditor.apply();

For the actual code involved with the issue also in the onCreate--

    final ImageView buy = (ImageView) findViewById(R.id.imageView26);
    final ImageView car = (ImageView) findViewById(R.id.imageView9);
    car.setTag("sportscar1");
    final ImageView use = (ImageView) findViewById(R.id.imageView5);
    buy.setImageResource(R.drawable.bull);
    if(car.getTag()=="sportscar1") {
        if (locked.equals("Locked")) {
            use.setImageResource(R.drawable.bull2);
        }
    }else{use.setImageResource(R.drawable.use);}
    final ImageView right = (ImageView) findViewById(R.id.imageView25);
    final ImageView left = (ImageView) findViewById(R.id.imageView24);

    buy.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (car.getTag() == "sportscar1") {
                if (locked.equals("Locked") && GameView.coinz >= 1) {
                    status1="Unlocked";
                    Colors.color1 = "sportscar1";
                    GameView.coinz = GameView.coinz - 1;
                    Toast.makeText(context, " - 1 Car Token",
                            Toast.LENGTH_SHORT).show();
                    Toast.makeText(context, "You just bought the sports car!",
                            Toast.LENGTH_LONG).show();

                    use.setImageResource(R.drawable.use);
                    buy.setImageResource(R.drawable.bull);
                } else {
                    Toast.makeText(context, " Not enough Car Tokens.",
                            Toast.LENGTH_SHORT).show();
                }
        }
    });

    left.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (car.getTag() == "sportscar1") {
                buy.setImageResource(R.drawable.bull);
                use.setImageResource(R.drawable.bull2);
                car.setTag("sportscar2");
                if (locked2.equals("Locked")) {
                    car.setImageResource(R.drawable.sportscar2_locked);
                    buy.setImageResource(R.drawable.buy);
                    use.setImageResource(R.drawable.bull2);
                } else if (locked2.equals("Unlocked")) {
                    car.setImageResource(R.drawable.sportscar2_show);
                    buy.setImageResource(R.drawable.bull);
                    use.setImageResource(R.drawable.use);
                }
        }
    });

    right.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (car.getTag() == "sportscar1") {
                buy.setImageResource(R.drawable.bull);
                use.setImageResource(R.drawable.bull2);
                car.setTag("thecar1");
                if (locked5.equals("Locked")) {
                    car.setImageResource(R.drawable.thecar1_locked);
                    buy.setImageResource(R.drawable.buy);
                    use.setImageResource(R.drawable.bull2);
                } else if (locked5.equals("Unlocked")) {
                    car.setImageResource(R.drawable.thecar1_show);
                    buy.setImageResource(R.drawable.bull);
                    use.setImageResource(R.drawable.use);

                }

            }
        }
    });

    use.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            if (car.getTag() == "sportscar2") {
                Colors.color1 = "sportscar2";

                status2="Unlocked";
                Toast.makeText(context, "You just selected the second sports car!",
                        Toast.LENGTH_LONG).show();

                buy.setImageResource(R.drawable.bull);
                use.setImageResource(R.drawable.use);
            }
        }
    });

Variables instantiated(At Activity beginning) --

 private String savelocked = "Locked or Unlocked";
private String savelocked2 = "Locked or Unlocked";
private String savelocked3 = "Locked or Unlocked";
private String savelocked4 = "Locked or Unlocked";
private String savelocked5 = "Locked or Unlocked";

public String status1 = "Locked";
public String status3 = "Locked";
public String status2 = "Locked";
public String status4 = "Locked";
public String status5 = "Locked";

public String locked;
public String locked2;
public String locked3;
public String locked4;
public String locked5;

private static SharedPreferences prefs;

Thanks for any help. I did not have this problem earlier when I was using ==, and this time I am using .equals(). Sorry for the lengthy code. And please help. All help is appreciated. Thank you for your time. Edit -- A lot of the code has been cut-off and the code above refers to the general meaning. Edit 2 -- Variables instantiated also included. Again Thanks for Your Help.

(sorry to add this as an answer, but I don't have enough rep to add a comment) There are other lines where you should be using "equals()" too:

In every place where you have:

if (car.getTag()=="sportscar1") { ...

(or " sportscar2 ", or whatever string) you should be using

if (car.getTag().equals("sportscar1")) { ...

Keep in mind that strings in Java are also objects, thus they cannot be compared like that, because they aren't the exact same instance of the same object. Therefore you should be using .equals()

You're welcome, Game Programmer. The error you were getting means that a preference variable with that name is there but, for some reason, the data type it's finding isn't a string.

Changing the code in your onCreate to:

status1 = prefs.getString("savelocked", "Locked");

and the ones in your onDestroy to:

prefsEditor.putString("savelocked", status1);

just stores and retrieves the same information in a different preference variable. Honestly, from what I can see in your code, I still have no idea why you were getting that error in the first place. But if it works, it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM