简体   繁体   中英

Error with onclick method in a fragment

I use a tablayout with a viewpager, in which I call three fragments. I have an error with one of those 3 fragments, this one : a part of equasec_calc.xml :

<RelativeLayout 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="fr.djey.maths.EquasecCalc">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true"
        android:gravity="center">

        <EditText
            android:text=""
            android:inputType="numberSigned|numberDecimal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/valuea"
            />

        <EditText
            android:text=""
            android:inputType="numberSigned|numberDecimal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/valueb"/>

        <EditText
            android:text=""
            android:inputType="numberSigned|numberDecimal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/valuec"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="apply"
            android:onClick="apply"/>

    </LinearLayout>

</RelativeLayout>

And a part of its java: EquasecCalc.java :

public void apply(View v) {  
    variablea = Double.parseDouble(valuea.getText().toString());
    variableb = Double.parseDouble(valueb.getText().toString());
    variablec = Double.parseDouble(valuec.getText().toString());
    Equasec Equasec = new Equasec();
    List<Double> result = Equasec.equasecCalc(variablea, variableb, variablec);

    size = result.size();
    if(size==1) {
        x = result.get(0);
    }
    else if(size==2) {
        x = result.get(0);
        y = result.get(1);
    }

    flag = 1;

    EquasecResult.Result();
}

Logcat:

01-18 21:43:46.531 15462-15462/fr.djey.maths E/AndroidRuntime: FATAL EXCEPTION: main
      Process: fr.djey.maths, PID: 15462
      java.lang.IllegalStateException: Could not find method apply(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
          at android.view.View.performClick(View.java:5198)
          at android.view.View$PerformClick.run(View.java:21147)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:148)
          at android.app.ActivityThread.main(ActivityThread.java:5417)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-18 21:43:52.049 15462-15468/fr.djey.maths W/art: Suspending all threads took: 16.749ms

As you can see in the title, I get an error clicking on the button with the onclick attribute apply . Can you tell me how I can do to do onclick action inside fragment like that?

If you think my error could come from another file, tell me and I will put the other files.

Thanks

android:onClick适用于API级别4及更高版本,因此,如果您要定位到以下级别,则不能使用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM