0

I want to check that if my radio button is checked than on another activity it should use local ip and if my radio button is not checked then it should use ip provided by user i m doing it through bundle. I used following code but if condition is giving error Required boolean Found java.lang.String so how could i do this thing. I m beginner in Android Studio so don't know much.

Following is the code i used:

SelectRoomActivity

if(rb.isChecked()){
    rb.setChecked(true);
    SelectRoomActivity.bundle.putString("local","true");
}else{
    rb.setChecked(false);
    SelectRoomActivity.bundle.putString("local","false");
}

EntranceActivity

if (SelectRoomActivity.bundle.getString("false")){   //error is generated if condition
        serverAdress = address;   //abc.ddns.net
    }
    else{
        serverAdress = lan;   //192.168.1.101
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • You are passing String by the name `local`" and trying to get it in another activity by the name `false`. It will surely give an error. – nimi0112 Nov 16 '18 at 10:23
  • If `SelectRoomActivity.bundle` is a static variable, that is a poor configuration for storing constants or configurations – OneCricketeer Nov 16 '18 at 10:25
  • Please read https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – OneCricketeer Nov 16 '18 at 10:27
  • if you insist on using strings like this, at least change SelectRoomActivity.bundle.getString("false") to SelectRoomActivity.bundle.getString("local") to grab the right string – koksalb Nov 16 '18 at 10:28

3 Answers3

4

Try that In you'r code you'r put String into your bundle, you need to put boolean When you get your variable in Entrance Activity, the string used are the name of you'r Boolean in the bundle ("local", and no "false")

SelectRoomActivity

if(rb.isChecked()){
    rb.setChecked(true);
    SelectRoomActivity.bundle.putBoolean("local",true);
}
else{
    rb.setChecked(false);
    SelectRoomActivity.bundle.putBoolean("local",false);
}

EntranceActivity

if (SelectRoomActivity.bundle.getBoolean("local")){   //error is generated if condition
    serverAdress = address;   //abc.ddns.net
}
else{
    serverAdress = lan;   //192.168.1.101
}
Benjamin
  • 401
  • 1
  • 3
  • 16
1

Change this

SelectRoomActivity.bundle.putString("local","true"); AND

SelectRoomActivity.bundle.putString("local","false");

TO

SelectRoomActivity.bundle.putBoolean("local",true); AND 

SelectRoomActivity.bundle.putBoolean("local",false);

And on NExt Activity

if (SelectRoomActivity.bundle.getBoolean("local")){  
        serverAdress = address;   //abc.ddns.net
    }
    else{
        serverAdress = lan;   //192.168.1.101
    }
Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
0

SelectRoomActivity

if(rb.isChecked()){
   rb.setChecked(true);
   SelectRoomActivity.bundle.putString("local","true");
}else{
   rb.setChecked(false);
   SelectRoomActivity.bundle.putString("local","false");
}

and in EntranceActivity

if (SelectRoomActivity.bundle.getString("local").equalsIgnoreCase("false")){   
   //error is generated if condition
    serverAdress = address;   //abc.ddns.net
}
else{
    serverAdress = lan;   //192.168.1.101
}
Basi
  • 3,009
  • 23
  • 28