nantitv
nantitv

Reputation: 3733

java.lang.IllegalStateException: Could not find method onCreate_Clicked(View) in a parent or ancestor Context for android:onClick

I created a Fragment using Android Studio 3.6.2. Fragment has a Button called ‘create’. But when I click on that button( even though I had linked button to that method) it fails with

java.lang.IllegalStateException: Could not find method onCreate_Clicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'create'

Click method of Button in Fragment

public void onCreate_Clicked(View caller) {
    create = getView().findViewById(R.id.create);
    create.setEnabled(true);
}

Button part in corresponding xml

<Button
    android:id="@+id/create"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="328dp"
    android:layout_marginEnd="240dp"
    android:onClick="onCreate_Clicked"
    android:text="Create"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

I did see similar questions but none of the answer in those post did not help. Could someone please advise

Upvotes: 0

Views: 1062

Answers (1)

Android_id
Android_id

Reputation: 1591

If you have an activity you can declare the onclick (as you declaredandroid:onClick="onCreate_Clicked")attribute in XML and can call the method but in fragmentandroid:onClick="onCreate_Clicked" will not called .

Possible Solutions:

  1. First case you have to declare a method onCreate_Clickedin your activity from where you are calling the fragment.
  2. Implement onClickListener programatically to handle the click event in button.

Hope this will help you..

Upvotes: 1

Related Questions