Skip to content Skip to sidebar Skip to footer

Java.lang.illegalstateexception: Could Not Find Method In A Parent Or Ancestor Context For Android:onclick Attribute

I'm trying to add the onClick method front() to my Button. However when I click on the Button it returns this error: java.lang.IllegalStateException: Could not find method front(Vi

Solution 1:

Be sure to write the grammar of the attribute value onClick correctly. In my case I was forgetting the character }

<androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:clickable="true"android:focusable="true"android:onClick="@{() -> viewModel.onItemClick(position)}">

GL

Solution 2:

Your front method in your activity should be public. You have made it private right now.

This is described in Android Developer site too.

In order for this to work, the method must be public and accept a View as its only parameter

publicvoidfront(View v){
    Toast.makeText(Register.this, "String", Toast.LENGTH_LONG).show();
}

Solution 3:

This is how a click action is given to button through activity.

front.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                Toast.makeText(this, "String", Toast.LENGTH_LONG).show();

            }
        });

To give button action through xml onClick,

publicvoidfront(View v) {
                Toast.makeText(this, "String", Toast.LENGTH_LONG).show();
}

Post a Comment for "Java.lang.illegalstateexception: Could Not Find Method In A Parent Or Ancestor Context For Android:onclick Attribute"