片段中的Android RadioButtons

时间:2016-07-16 00:38:21

标签: java android android-layout android-fragments android-radiobutton

我正在关注Radio Buttons教程,并希望在片段中创建一些RadioGroups RadioButtons。我定义了onClick方法但是如果我单击RadioButton则会出现错误:

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是否存在问题或我的代码不正确?

1 个答案:

答案 0 :(得分:3)

当您在XML中使用android:onClick标记时,Android只会在当前Activity中查找指定的onClick方法。它没有任何碎片。

最简单的选择是以编程方式分配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();
        }
    }
}