Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RadioButtons in Fragment

I am following Radio Buttons tutorial and want to create some RadioGroups with RadioButtons in fragment. I defined onClick method but if I click on RadioButton there is an error:

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 from fragment:

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 some problem with RadioButtons in fragment or my code is incorrect?

like image 501
barmi Avatar asked Jul 16 '16 00:07

barmi


People also ask

How to use RadioButton in android?

RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user. RadioButton is generally used with RadioGroup.

How to declare Radio button in android?

To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

How to use Radio button group in android Studio?

For this, open the “MainActivity. java” file and instantiate the components made in the XML file (RadioGroup, TextView, Clear, and Submit Button) using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID. Button submit = (Button)findViewById(R.

What is the purpose of Radio button?

Radio buttons allow the user to select one option from a set. Use radio buttons when the user needs to see all available options. If available options can be collapsed, consider using a dropdown menu because it uses less space. Selected and unselected radio buttons.


1 Answers

When you use the android:onClick tag in your XML, Android will only look in your current Activity for the specified onClick method. It does not look in any fragments.

The simplest option is to assign the onClick programmatically.

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();
        }
    }
}
like image 145
Sound Conception Avatar answered Sep 27 '22 18:09

Sound Conception