确定在 RadioGroup 中检查哪个按钮时出现 InvokingTargetException

发布于 2024-11-25 11:41:09 字数 1143 浏览 1 评论 0原文

我有一个应用程序,它根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流云如水 2024-12-02 11:41:09

让我说一下我的想法。由于您正在使用 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.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文