Reputation: 3733
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
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:
onCreate_Clicked
in your activity from where you are calling the fragment.onClickListener
programatically to handle the click event in button.Hope this will help you..
Upvotes: 1