Antoniossss
Antoniossss

Reputation: 32517

Why Android cannot find my onClick method inside fragment?

So I have simple button in fragment, and I have an exception while clicking it. Fragmens shows up just fine.

Fragment code:

    public void onApplyButtonPressed(View view) {
        System.out.println("Apply button clicked");
//        viewToFilterData();
//        if (mListener != null) {
//            mListener.onFiltersApplyButtonPressed(userData);
//        }
    }

XML layout of fragment

<Button
    android:id="@+id/fragment_filters_apply_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:onClick="onApplyButtonPressed"
    android:text="@string/fragment_filters_apply_button" />

And how I attach my fragment in my main activity:

FiltersFragment filtersFragment = FiltersFragment.newInstance(userData);
getSupportFragmentManager().beginTransaction().add(R.id.activity_main_filters_page, filtersFragment).commit();

Can anyone see why this error happens?

 java.lang.IllegalStateException: Could not find method onApplyButtonPressed(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'fragment_filters_apply_button'
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
    at android.view.View.performClick(View.java:4438)
    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:5017)
    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)

PS: Using the same method as consumer to View.OnClickListener#onClick(View) works just fine. Something is wrong with method lookup in current context.

Upvotes: 2

Views: 1843

Answers (3)

Valeriy
Valeriy

Reputation: 305

You inflate fragment from your MainActivity, so it puts in the main layout so the context for your xml is activity that instantiates your fragment. Make sure that you have onApplyButtonPressed(View v)method in your MainActivity.

Upvotes: 0

Denys Vasylenko
Denys Vasylenko

Reputation: 2143

The onClick attribute is intended to be used with Activities NOT Fragments. Looks like the using of OnClickListener is the simplest solution in your case. Another option (my favorite) is to use ButterKnife library which allows you to deal with that routine using annotations:

@OnClick(R.id.fragment_filters_apply_button)
void onApplyButtonClick(View view){
//your code
}

Upvotes: 2

Maik Peschutter
Maik Peschutter

Reputation: 613

You can not handle the click event in your fragment by refer a method in XML. This only works if the method is implemented in your activity. I think it‘s because the activity ist the context of inflated view but the view doesn‘t have any reference to the fragment.

And this is why the exception is thrown because the method is missing in your activity.

You should use findViewById().setOnClickListener() inside your fragment.

Upvotes: 5

Related Questions