簡體   English   中英

在 android:onClick 屬性在視圖 class 上定義的父或祖先上下文中找不到方法 log_out(View)

[英]Could not find method log_out(View) in a parent or ancestor Context for android:onClick attribute defined on view class

我試圖在 Fragment 中做一個簡單的注銷過程。 但在此之前,我遇到了這個錯誤:在 android:onClick 視圖上定義的屬性 class androidx.appcompat.widget.AppCompatButton上定義的屬性 android 的父或祖先上下文中error: Could not find method log_out(View)

這是我的 ProfileFragment.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(LayoutInflater inflater, ViewGroup 容器, 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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM