java-片段中的Android RadioButtons

java-片段中的Android RadioButtons,第1张

概述我正在阅读RadioButtons教程,并想创建一些带有RadioButtons片段的RadioGroup.我定义了onClick方法,但是如果我单击RadioButton,就会出现错误:java.lang.IllegalStateException:CouldnotfindmethodFirstQuestion(View)inaparentorancestorContextforandroid:onClick

我正在阅读Radio buttons教程,并想创建一些带有Radiobuttons片段的RadioGroup.我定义了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();    }  }}

片段中的单选按钮是否存在某些问题,或者我的代码不正确?

解决方法:

当您在XML中使用androID:onClick标记时,AndroID只会在当前活动中查找指定的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();        }    }}
总结

以上是内存溢出为你收集整理的java-片段中的Android RadioButtons全部内容,希望文章能够帮你解决java-片段中的Android RadioButtons所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1120795.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存