Android RadioButtons in Java fragment
•
Android
I am reading the radio buttons tutorial and want to create some radiogroups with radio buttons clips. I have defined the onclick method, but if I click the radio button, an error will appear:
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)
My XML file is:
<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>
And code in fragments:
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();
}
}
}
Is there something wrong with the radio buttons in the clip, or is my code incorrect?
resolvent:
When you use the Android: onclick tag in XML, Android will only find the specified onclick method in the current activity. It will not appear any fragments
The simplest option is to programmatically assign onclick
public class question1from10 extends Fragment implements View.OnClickListener {
public question1from10() {
// required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_question1from10, container, false);
// Set the onClick for each of our views as the one implemented by this Fragment
rootView.findViewById(R.id.test1from10question1answerA).setOnClickListener(this);
rootView.findViewById(R.id.test1from10question1answerB).setOnClickListener(this);
rootView.findViewById(R.id.test1from10question1answerC).setOnClickListener(this);
...
return rootView;
}
@Override
public void onClick(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();
}
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码