java - 无法从 firestore 获取数据

标签 java android google-cloud-firestore

我无法从我的用户的 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/

相关文章:

java - 运行jetty而不使用maven 2插件打包项目

java - Java(android) 如何在另一个方法中使用一个方法的变量

android - 错误 :(41, 13) 无法解析 : de. codecrafters.tableview :tableview:0. 9.5

java - 在Java中从HashMap中检索对象?

java - Spring MySQL Profiler 抛出异常

java - 转换为 JScrollPane 中 JTable 的图像

android - 如何以编程方式更改 Android 中 Spinner 的颜色?

flutter - 如何使用 StreamProvider 检查 Firestore 快照流的 ConnectionState?

javascript - 从 Firebase Firestore 读取时无法写入值。说 "update is not a function"

flutter - 基于角色的授权和基于角色的访问控制 flutter