Android 在确定在放射组中选中哪个按钮时调用TargetException

Android 在确定在放射组中选中哪个按钮时调用TargetException,android,radio-button,radio-group,invocationtargetexception,Android,Radio Button,Radio Group,Invocationtargetexception,我有一个应用程序,它根据RadioGroup内选定的RadioButton确定执行的路径 当以下代码片段运行时,程序强制以调用TargetException关闭: private int getselctedRadioButtonId() { RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1); return group.getCheckedRadioButtonId(); } 我做错了什么 我可以发布更

我有一个应用程序,它根据
RadioGroup
内选定的
RadioButton
确定执行的路径

当以下代码片段运行时,程序强制以调用TargetException关闭:

private int getselctedRadioButtonId() {
    RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
    return group.getCheckedRadioButtonId();
}
我做错了什么

我可以发布更多的代码,但我认为这是有问题的部分

我的目标是安卓2.2

编辑:以下是主线程的完整堆栈跟踪:

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]
Thread[main](挂起(异常非法状态异常))
视图$1。单击(视图)行:2072
按钮(视图)。performClick()行:2408
查看$PerformClick.run()行:8816
ViewRoot(处理程序).handleCallback(消息)行:587
ViewRoot(处理程序)。dispatchMessage(消息)行:92
Looper.loop()行:123
ActivityThread.main(字符串[])行:4627
invokenactive(Object,Object[],Class,Class[],Class,int,boolean)行:不可用[本机方法]
调用(对象,对象…)行:521
ZygoteInit$MethodAndArgsCaller.run()行:868
颧骨单位。主(字符串[])行:626
NativeStart.main(字符串[])行:不可用[本机方法]

让我说说我的想法。因为您使用的是
RadioGroup
单选按钮
有一个选择。现在,重写onCheckedChanged(RadioGroup组,int checkedId)
并根据
checkedd
的值确定执行的路径。如果您根据组中的id(来自xml)或唯一id号执行
决策,则情况也是一样的。这对你有意义吗?我希望你明白我想说的,这也会有帮助。

InvocationTargetException包装了另一个异常,你能告诉我们底层异常是什么吗?@MByD我怎么做?我正在使用Eclipse和ADT插件。在Eclipse上调试它。当您得到异常时,请查看调试窗口。或者,查看debug/DDMS windows中的logcat框。InvocationTargetException是一个选中的异常,它封装了被调用的方法或构造函数引发的异常。那么,哪一行导致了异常?您是否检查了选择radiobutton时调用的方法?还发布stacktrace。@MByD它包装了一个
NullPointerException
,但就是这样,没有任何原因或与此异常相关的任何东西。它只是一个想法,可以避免异常和
group.getCheckedRadioButtonId()的使用这只是另一种方式。如果您使用
group.getCheckedRadioButtonId()尝试了我告诉您的内容可能无法工作,将引发相同的异常。不用。不,我没有将它与
getCheckedRadioButtonId()
一起使用,这是另一个问题,但它最终成功了,谢谢。很好。如果我知道的话,问题的原因是什么?(没有代码,只有文字):DI确实编写了处理事件的代码,但忘记在
onCreate()
:D中调用它