确定在 RadioGroup 中检查哪个按钮时出现 InvokingTargetException
我有一个应用程序,它根据 RadioGroup
内选定的 RadioButton
确定采用哪个执行路径。
当以下代码片段运行时,程序会强制关闭并出现 InitationTargetException
:
private int getselctedRadioButtonId() {
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
return group.getCheckedRadioButtonId();
}
我做错了什么?
我可以发布更多代码,但我认为这是有问题的部分。
我的目标是 Android 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]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我说一下我的想法。由于您正在使用
RadioGroup
,即RadioButtons
有一种选择。现在,重写 onCheckedChanged(RadioGroup group, int checkId)
并根据
checkedId
的值确定采取哪条执行路径。如果您根据 id(来自 xml)或组中的唯一 id 号进行决策
,情况是一样的。这对你来说有意义吗?我希望你能明白我想说的,这也有帮助。Let me say what I have in mind. Since you are working with
RadioGroup
i.eRadioButtons
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 thedecisions
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.