我尝试在homeactivity.java中使用片段。然后我将代码从旧的ProfileActivity.java复制到新的profilefragment.java中。然后我得到这个错误。其他一切都很好。 如何在片段中使用documentreference
这是我的fragment_profile.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light">
<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=".HomeActivity">
<TextView
android:id="@+id/verifyMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center_horizontal"
android:text="Email Not Verified!"
android:textColor="@android:color/holo_red_dark"
android:textSize="20dp"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp" />
<Button
android:id="@+id/resendCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:gravity="center_horizontal"
android:text="VERIFY NOW"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/verifyMsg" />
<TextView
android:id="@+id/profile_top_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="108dp"
android:layout_marginEnd="8dp"
android:text="Your Profile"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/profile_text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Your Name"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="@+id/imageView2" />
<TextView
android:id="@+id/profile_text_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Your Email Address"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/profile_text_username" />
<TextView
android:id="@+id/profile_text_phone_number"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Phone Number"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/profile_text_email" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="111dp"
android:layout_height="120dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profile_top_text"
app:layout_constraintVertical_bias="0.045"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/profile_text_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Usename"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/profile_text_name" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
这是我的ProfileFragment.java代码
package com.infinite.estudy;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
public class ProfileFragment extends Fragment {
TextView fullName, username, email, phoneNumber, verifyMsg;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userID;
Button resendCode;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_profile, container, false);
fullName = v.findViewById(R.id.profile_text_name);
username = v.findViewById(R.id.profile_text_username);
email = v.findViewById(R.id.profile_text_email);
phoneNumber = v.findViewById(R.id.profile_text_phone_number);
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
resendCode = v.findViewById(R.id.resendCode);
verifyMsg = v.findViewById(R.id.verifyMsg);
userID = fAuth.getCurrentUser().getUid();
final FirebaseUser user = fAuth.getCurrentUser();
DocumentReference documentReference = fStore.collection("Users").document(userID);
documentReference.addSnapshotListener(this,new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
email.setText(documentSnapshot.getString("email"));
fullName.setText(documentSnapshot.getString("fName"));
phoneNumber.setText(documentSnapshot.getString("phoneNumber"));
username.setText(documentSnapshot.getString("username"));
}
});
if(!user.isEmailVerified()){
resendCode.setVisibility(View.VISIBLE);
verifyMsg.setVisibility(View.VISIBLE);
resendCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
user.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(view.getContext(), "Verification Email Has Been Sent", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("tag","onFailure: Email not sent" + e.getMessage());
}
});
}
});
}
return v;
}
}
这是我得到的错误
错误:找不到适合的方法 addSnapshotListener(ProfileFragment,>)方法 DocumentReference.addSnapshotListener(Executor,EventListener) 不适用(参数不匹配; ProfileFragment不能为 转换为执行器)方法 DocumentReference.addSnapshotListener(Activity,EventListener) 不适用(参数不匹配; ProfileFragment不能为 转换为Activity)方法 DocumentReference.addSnapshotListener(MetadataChanges,EventListener) 不适用(参数不匹配; ProfileFragment不能为 转换为MetadataChanges)
我将片段放入homeactivity.java
答案 0 :(得分:0)
而不是在DocumentRefrence中通过getactivity()可能会解决您的问题
DocumentReference documentReference = fStore.collection("Users").document(userID);
documentReference.addSnapshotListener(getActivity(),new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
email.setText(documentSnapshot.getString("email"));
fullName.setText(documentSnapshot.getString("fName"));
phoneNumber.setText(documentSnapshot.getString("phoneNumber"));
username.setText(documentSnapshot.getString("username"));
}
});