无法从Firestore获取数据

时间:2020-06-02 05:53:23

标签: java android google-cloud-firestore

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

我的日志

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收藏

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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