gpt4 book ai didi

java - 无法从 firestore 获取数据

转载 作者:行者123 更新时间:2023-12-01 16:26:03 28 4
gpt4 key购买 nike

我无法从我的用户的 firestore 获取数据。它在我的 TextView 中给我空白。我不知道这个错误是什么,但我也无法在 Activity 之间传递数据,相信我,我做的一切都是正确的。我还添加了一个 toast 来检查用户 ID,它确实显示我的用户 ID。无论如何,数据都不会显示。我在其他项目中以相同的方式使用了 firestore,并且在那里工作得很好。我也尝试创建一个新项目,但我无法获取数据来自 Activity 外部。

我的logcat

2020-06-02 11:15:52.749 19990-19990/? E/Zygote: v2
2020-06-02 11:15:52.750 19990-19990/? E/Zygote: accessInfo : 0
2020-06-02 11:15:58.778 19990-19990/com.example.feedme2 E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]

我的java类

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
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;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

public class profile extends AppCompatActivity {

TextView fullname;
CircleImageView circleImageView;
BottomNavigationView bottomNavigationView;
String user_id;
String url;
FirebaseFirestore fStore;
FirebaseAuth fAuth;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);


circleImageView = findViewById(R.id.profilepictureuser);
Picasso.with(this).load(url).resize(400, 400).centerCrop().into(circleImageView);
fullname = findViewById(R.id.profilename);
fStore = FirebaseFirestore.getInstance();
fAuth = FirebaseAuth.getInstance();
user_id = fAuth.getCurrentUser().getUid();

Toast.makeText(this, user_id, Toast.LENGTH_SHORT).show();


DocumentReference documentReference = fStore.collection("User data").document(user_id);
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot documentSnapshot,FirebaseFirestoreException e) {
fullname.setText(documentSnapshot.getString("Full name"));
}
});

bottomNavigationView = findViewById(R.id.bottom_navigation1);
bottomNavigationView.setSelectedItemId(R.id.profilelogo);



bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.profilelogo:
return true;
case R.id.home:
startActivity(new Intent(getApplicationContext(), homepage.class));
return true;

}
return false;
});


}
}

我的布局文件


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".homepage">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearlayout"
android:orientation="vertical">

<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/profile"
android:id="@+id/profilepictureuser"
android:layout_gravity="center"
android:layout_marginTop="30dp"/>

<TextView
android:id="@+id/profilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:textColor="#000000"
android:textSize="24sp" />



</LinearLayout>


<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottom_navigation1"
android:background="#047cfb"
app:itemIconTint="#ffffff"
app:itemTextColor="#ffffff"
app:menu="@menu/menu_navigation"
android:layout_alignParentBottom="true"
/>

</RelativeLayout>

我的 Firestore 规则

service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}

我的 firestore Collection

enter image description here

最佳答案

尝试运行代码以在主线程上获取数据。

new Handler(Looper.getMainLooper()).post(new Runnable () {
@Override
public void run () {
// your code here
}
});

关于java - 无法从 firestore 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62145687/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com