【问题标题】:InvocationTargetException when determining which button is checked in a RadioGroup确定在 RadioGroup 中检查了哪个按钮时出现 InvocationTargetException
【发布时间】:2011-07-23 11:06:02
【问题描述】:

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

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

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]

【问题讨论】:

  • InvocationTargetException 包装了另一个异常,你能告诉我们底层异常是什么吗?
  • @MByD 我该怎么做?我正在使用 Eclipse 和 ADT 插件。
  • 在 Eclipse 上调试它。当您收到异常时,请查看“调试”窗口。或者,查看调试/DDMS 窗口中的 logcat 框。
  • InvocationTargetException 是一个经过检查的异常,它包装了被调用的方法或构造函数抛出的异常。那么哪一行导致异常?您是否检查了选择单选按钮时调用的方法?同时发布堆栈跟踪。
  • @MByD 它包装了一个NullPointerException 但就是这样,没有任何原因或任何与此异常相关的东西。

标签: android radio-button radio-group invocationtargetexception


【解决方案1】:

让我说说我的想法。由于您正在使用RadioGroup,即RadioButtons 有一种选择。现在,覆盖onCheckedChanged(RadioGroup group, int checkedId) 并根据checkedId 的值确定要采取的执行路径。如果您根据 id(来自 xml)或组中的唯一 ID 号执行 decisions,则相同。这对你有意义吗?我希望你能得到我想说的,它也有帮助。

【讨论】:

  • 这只是一个想法,可能会避免group.getCheckedRadioButtonId(); 的异常和用法这只是另一种方式。如果您尝试了我告诉您的内容,group.getCheckedRadioButtonId(); 可能不起作用,将引发相同的异常。不这样做。
  • 不,我没有和getCheckedRadioButtonId()一起使用,这是另一个问题,但它终于奏效了,谢谢。
  • 好。如果我可能知道,问题的原因是什么? (没有代码,只有文字):D
  • 我确实编写了处理事件的代码,但忘记在onCreate() 中调用它:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多