在视图类上定义的 android:onClick 属性的父级或上级上下文中找不到方法 log_out(视图)



我试图在Fragment中做一个简单的注销过程。但在此之前,我在父级或祖先上下文中采用此error: Could not find method log_out(View)在 ID 为"AppCompatButton"的视图类androidx.appcompat.widget.AppCompatButton上定义的 android:onClick 属性

这是我的个人资料片段.java package com.example.yemektarifi;

import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.firebase.auth.FirebaseAuth;

public class ProfileFragment extends Fragment {
FirebaseAuth firebaseAuth;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_profile, container, false);
}
public void log_out(View view){
firebaseAuth.signOut();
Intent intent  = new Intent(ProfileFragment.this.getActivity(), LoginScreen.class);
startActivity(intent);
}
}

这是fragment_profile.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProfileFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView13"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Profile Fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/AppCompatButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="207dp"
android:background="@drawable/custom_button"
android:gravity="center"
android:onClick="log_out"
android:text="@string/log_out"
android:textColor="#CADCDA"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我用ViewGroup解决了这个问题。

@Override public View onCreateView(LayoutInflower inflater, ViewGroup container, Bundle savedInstanceState( {

final ViewGroup viewGroup =(ViewGroup)inflater.inflate(R.layout.fragment_add,container,false);
firebaseAuth = firebaseAuth.getInstance();
button = viewGroup.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firebaseAuth.signOut();
startActivity(new Intent(ProfileFragment.this.getContext(), LoginScreen.class));
}
});
return viewGroup;

}