Mahmoud Hanafy
Mahmoud Hanafy

Reputation: 8228

InvocationTargetException when determining which button is checked in a RadioGroup

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]

Upvotes: 0

Views: 763

Answers (1)

Nikola Despotoski
Nikola Despotoski

Reputation: 50578

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.

Upvotes: 1

Related Questions