0

I have an application that determines which path of execution to take based on the selected RadioButton inside a RadioGroup.

The program force closes with an InvocationTargetException when the following fragment of code runs :

private int getselctedRadioButtonId() {
    RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
    return group.getCheckedRadioButtonId();
}

What am I doing wrong?

I can post more code, but I think this is the problematic part.

I'm targeting Android 2.2.

Edit: here's the full stack trace for the main thread:

Thread [<1> main] (Suspended (exception IllegalStateException)) 
View$1.onClick(View) line: 2072 
Button(View).performClick() line: 2408  
View$PerformClick.run() line: 8816  
ViewRoot(Handler).handleCallback(Message) line: 587 
ViewRoot(Handler).dispatchMessage(Message) line: 92 
Looper.loop() line: 123 
ActivityThread.main(String[]) line: 4627    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 521  
ZygoteInit$MethodAndArgsCaller.run() line: 868  
ZygoteInit.main(String[]) line: 626 
NativeStart.main(String[]) line: not available [native method]
Mahmoud Hanafy
  • 7,958
  • 12
  • 47
  • 62
  • InvocationTargetException wraps another exception, can you tell us what's the underlying exception? – MByD Jul 23 '11 at 01:51
  • @MByD how can I do that? I'm using Eclipse and the ADT plugin. – Mahmoud Hanafy Jul 23 '11 at 02:27
  • Debug it on Eclipse. when you get the exception look at the Debug window. Or, look at the logcat box in the debug / DDMS windows. – MByD Jul 23 '11 at 02:31
  • InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor. So which line causes the exception? Did you check the method that is invoked when radiobutton is selected? Also post the stacktrace. – Caner Jul 23 '11 at 02:31
  • @MByD it wraps a `NullPointerException` but that's it, there's no cause or anything associated with this exception whatsoever. – Mahmoud Hanafy Jul 23 '11 at 02:45
  • before `getselctedRadioButtonId` is called, do you call setContentView? – MByD Jul 23 '11 at 02:47
  • Just a question from scenario (NPE => none is checked): Is any of the radio buttons checked `at all`? Ensure if any is checked before you return. – Nikola Despotoski Jul 23 '11 at 02:52
  • @MByD setContentView is called in the onCreate method, this is called way before all this happens, it gets called once my application starts. – Mahmoud Hanafy Jul 23 '11 at 02:52
  • @Nikola Yes, I tried to select another radio button, same problem. – Mahmoud Hanafy Jul 23 '11 at 02:53

1 Answers1

1

Let me say what I have in mind. Since you are working with RadioGroup i.e RadioButtons There is one option. Now, override onCheckedChanged(RadioGroup group, int checkedId) and based on the value of checkedId determine which path of execution to take. It's the same, if you do the decisions based on the id's(from the xml) or unique id number in the group. Does this makes sense to you? I hope you get what I want to say and it helps as well.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • It just an idea, that might avoid the exception and usage of `group.getCheckedRadioButtonId();` It's just another way around. If you tried what i told you WITH `group.getCheckedRadioButtonId();` probably wont work, same exception will be thrown. Do it without. – Nikola Despotoski Jul 24 '11 at 00:10
  • No, I didn't use it with `getCheckedRadioButtonId()`, it was another problem, but it finally worked, thank you. – Mahmoud Hanafy Jul 24 '11 at 02:47
  • Good. What was the cause of the problem, if I may know? (no code, just words) :D – Nikola Despotoski Jul 24 '11 at 02:50
  • I did write the code that handles the event, but forgot to invoke it in `onCreate()` :D – Mahmoud Hanafy Jul 24 '11 at 05:02