Reputation: 681
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
Upvotes: 0
Views: 1024
Reputation: 638
android:onClick is for API level 4 and onwards, so if you're targeting below then this you can't use it.
Upvotes: 1