0

i m creating app in which i m taking Radio Button Value From RadioGroup when i m Comparing the Value in if Statement it is not working. Here is Java code that i m using.

if(radioQuizeGroup.getText() == "History"){
    Log.d("History", "Activity");
    Toast.makeText(StartQuize.this,"History sss ss ", Toast.LENGTH_SHORT).show();    
}
else{
    Log.d("History", "Error ");
}

and i m getting Error in Error log,
here is Error Log

01-05 07:15:02.885: D/gralloc_goldfish(2035): Emulator without GPU emulation detected.
01-05 07:15:04.165: I/Choreographer(2035): Skipped 43 frames!  The application may be doing too much work on its main thread.
01-05 07:15:05.245: D/dalvikvm(2035): GC_FOR_ALLOC freed 86K, 5% free 3139K/3280K, paused 18ms, total 32ms
01-05 07:15:15.236: D/History(2035): Error 

Please Help me

Ashu Kumar
  • 832
  • 19
  • 38

3 Answers3

5

It is recommended to use

if(radioQuizeGroup.getText().equals("History"))

rather than

if(radioQuizeGroup.getText() == "History")

Since the == compares the location of the objects and .equals() compares the value of the objects

R9J
  • 6,605
  • 4
  • 19
  • 25
  • 2
    Putting history first will also protect the code against NPE is getText returns null. – 2Dee Jan 05 '15 at 12:22
  • @R9J can u please tell me how to open a new box Area after selecting a Radio box where you can place a EditBox to enter Numbers help will appreciate – Ashu Kumar Jan 05 '15 at 12:37
  • @AshuKumar You can call a new Activity when option is clicked – R9J Jan 05 '15 at 12:41
  • but i donot want to see a New Screen , is there a any way to swho new Activity in Existing Activity ? – Ashu Kumar Jan 05 '15 at 12:44
  • Then use `PopupWindow`, for more http://mrbool.com/how-to-implement-popup-window-in-android/28285 – R9J Jan 05 '15 at 12:51
2

Never compare String using the == operator, use the equals method instead :

if("History".equals(radioQuizeGroup.getText())){
    Log.d("History", "Activity");
    Toast.makeText(StartQuize.this, "History sss ss ", Toast.LENGTH_SHORT).show();
} else {
    Log.d("History", "Error ");
}

This question has a nice answer that explains this a bit more.

Of course this assumes radioQuizeGroup is actually a widget that has a getText method as RadioGroup doesn't...

Community
  • 1
  • 1
2Dee
  • 8,609
  • 7
  • 42
  • 53
  • can u please tell me how to open a new box Area after selecting a Radio box where you can place a EditBox to enter Numbers help will appreciate – Ashu Kumar Jan 05 '15 at 12:39
0

I would check, by using the selected item id like below

int selectedId = radioQuizeGroup.getCheckedRadioButtonId();
if(selectedId == RadioButton1.getId()) //Id of the RadioButton1
 {
 //do something here
 } 
 else 
{
 //else do something here
 }
Karthikeyan
  • 1,119
  • 1
  • 15
  • 33