3
我正在關注單選按鈕教程,並希望在片段中創建一些RadioGroups
與RadioButtons
。我定義的onClick方法,但如果我點擊單選按鈕有一個錯誤:片段中的Android RadioButtons
java.lang.IllegalStateException: Could not find method FirstQuestion(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatRadioButton with id 'test1from10question1answerA'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:4438)
at android.widget.CompoundButton.performClick(CompoundButton.java:100)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
我的XML文件是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.plan.aplikacjamobilna.registerTestFragments.question1from10"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1:"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:id="@+id/test1from10question1answerA"
android:onClick="FirstQuestion"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:id="@+id/test1from10question1answerB"
android:onClick="FirstQuestion"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/test1from10question1answerC"
android:onClick="FirstQuestion"/>
</RadioGroup>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2:"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:id="@+id/test1from10question2answerA"
android:onClick="SecondQuestion"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:id="@+id/test1from10question2answerB"
android:onClick="SecondQuestion"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/test1from10question2answerC"
android:onClick="SecondQuestion"/>
</RadioGroup>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3:"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:id="@+id/test1from10question3answerA"
android:onClick="ThirdQuestion"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:id="@+id/test1from10question3answerB"
android:onClick="ThirdQuestion"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/test1from10question3answerC"
android:onClick="ThirdQuestion"/>
</RadioGroup>
</LinearLayout>
和代碼片段:
public class question1from10 extends Fragment {
public question1from10() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_question1from10, container, false);
}
public void FirstQuestion(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()){
case R.id.test1from10question1answerA:
Toast.makeText(getActivity(), "A", Toast.LENGTH_LONG).show();
case R.id.test1from10question1answerB:
Toast.makeText(getActivity(), "B", Toast.LENGTH_LONG).show();
case R.id.test1from10question1answerC:
Toast.makeText(getActivity(), "C", Toast.LENGTH_LONG).show();
}
}
}
是有一些問題與RadioButtons
在片段或我的代碼是不正確的?
請發佈錯誤日誌,也嘗試將FirstQuestion方法移到父Activity,看它是否有效。 –