我有一个应用程序,允许用户从RadioButtons列表和DatePicker中选择并显示输出。我让应用程序在模拟器中运行,当我测试RadioButtons时,应用程序崩溃,模拟器说“不幸的是应用程序停止了”。我不确定为什么会这样?
编辑 - 堆栈跟踪
堆栈跟踪:
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: FATAL EXCEPTION: main
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: java.lang.IllegalStateException: Could not find a method onRadioButtonClicked(View) in the activity class edu.wmich.lab4jvandyk5713.MainActivity for onClick handler on view class android.widget.RadioButton with id 'radioButtonHoliday'
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.view.View$1.onClick(View.java:3586)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.view.View.performClick(View.java:4204)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.widget.CompoundButton.performClick(CompoundButton.java:100)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17355)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: Caused by: java.lang.NoSuchMethodException: onRadioButtonClicked [class android.view.View]
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at java.lang.Class.getConstructorOrMethod(Class.java:460)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at java.lang.Class.getMethod(Class.java:915)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.view.View$1.onClick(View.java:3579)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.view.View.performClick(View.java:4204)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.widget.CompoundButton.performClick(CompoundButton.java:100)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17355)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-01 00:30:39.490 2007-2007/edu.wmich.lab4jvandyk5713 E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
编辑2 - 有问题的XML
<?xml version="1.0" encoding="utf-8"?>
<TableRow>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewUltimateParty"
android:contentDescription="@string/imgViewUltimateParty"
android:longClickable="true"
android:paddingBottom="16dp"
android:paddingRight="16dp"/>
<TableRow>
<TextView
android:layout_width="500dp"
android:layout_height="wrap_content"
android:text="@string/txtViewAppDescription"
android:id="@+id/textViewAppDescription"
android:contentDescription="@string/txtViewAppDescription"
android:contextClickable="false" />
</TableRow>
</TableRow>
<TableRow>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/rdoGroupParties"
android:id="@+id/radioGroupParties">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rdoButtonPajama"
android:id="@+id/radioButtonPajama"
android:contentDescription="@string/rdoButtonPajama"
android:onClick="onRadioButtonClicked"
android:enabled="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rdoButtonPizza"
android:id="@+id/radioButtonPizza"
android:contentDescription="Pizza Party"
android:onClick="onRadioButtonClicked"
android:enabled="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rdoButtonGraduation"
android:id="@+id/radioButtonGraduation"
android:contentDescription="@string/rdoButtonGraduation"
android:contextClickable="false"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rdoButtonRetirement"
android:id="@+id/radioButtonRetirement"
android:contentDescription="@string/rdoButtonRetirement"
android:onClick="onRadioButtonClicked" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rdoButtonHoliday"
android:id="@+id/radioButtonHoliday"
android:contentDescription="@string/rdoButtonHoliday"
android:onClick="onRadioButtonClicked" />
</RadioGroup>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnChooseDate"
android:id="@+id/buttonChooseDate" />
</TableRow>
<TableRow android:transitionGroup="false">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnNextStep"
android:id="@+id/buttonNextStep"
android:contentDescription="@string/btnNextStep" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textViewOutput"
android:contentDescription="@string/txtViewOutput"
android:layout_column="1" />
</TableRow>
任何帮助都将不胜感激。
答案 0 :(得分:2)
您需要定义方法:
public void onRadioButtonClicked(View v){
//... some logic or nothing
}
由于您在xml中定义android:onClick="onRadioButtonClicked"
,因此尝试调用此方法,因此单击视图时,您的活动中的方法需要一个名为this的方法,该方法将View
单击作为其唯一参数传递。 / p>