实时数据firebase数据库的java问题

rekjcdws  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(432)

我正在尝试从具有以下结构的数据库中检索所有uid。此图显示“users”每个用户的“uid”的子级列表,uid的子级为username,值为username,我无法访问数据库。

package com.example.androidChat;

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

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView userListView;
    ArrayList<String> UsernameArrayList, UidArrayList;
    private FirebaseAuth mAuth;
    FirebaseDatabase database;

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

        mAuth = FirebaseAuth.getInstance();
        userListView = findViewById(R.id.userListView);
        UsernameArrayList = new ArrayList<>();
        UidArrayList = new ArrayList<>();

        database = FirebaseDatabase.getInstance();
        DatabaseReference getUser = database.getReference().child("User");
        getUser.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot: snapshot.getChildren()){
                    Log.i("Users", dataSnapshot.getValue().toString());
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.i("Users", "Error over here!!");
                Log.w("Error: ", error.getMessage());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main_menu,menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.logout:
                newGame();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void newGame() {
        mAuth = FirebaseAuth.getInstance();
        mAuth.signOut();
        Intent intent = new Intent(this, StartActivity.class);
        startActivity(intent);
    }
}

我尝试添加一些依赖项并尝试了所有操作,但出现了以下错误。

implementation 'com.google.android.gms:play-services-basement:17.4.0'

上面这一行是在另一个问题中提出的建议,但不起作用。

2020-12-13 15:46:38.426 8679-9347/com.example.androidChat W/ConnectionTracker: Exception thrown while unbinding
    java.lang.IllegalArgumentException: Service not registered: lt@e163c21
        at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1466)
        at android.app.ContextImpl.unbindService(ContextImpl.java:1622)
        at android.content.ContextWrapper.unbindService(ContextWrapper.java:697)
        at ci.f(:com.google.android.gms.dynamite_measurementdynamite@204713052@20.47.13 (040700-0):1)
        at ci.d(:com.google.android.gms.dynamite_measurementdynamite@204713052@20.47.13 (040700-0):2)
        at lu.E(:com.google.android.gms.dynamite_measurementdynamite@204713052@20.47.13 (040700-0):9)
        at le.a(:com.google.android.gms.dynamite_measurementdynamite@204713052@20.47.13 (040700-0):3)
        at eg.run(:com.google.android.gms.dynamite_measurementdynamite@204713052@20.47.13 (040700-0):3)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at iz.run(:com.google.android.gms.dynamite_measurementdynamite@204713052@20.47.13 (040700-0):5)

我甚至试着改变模拟器并在自己的设备上运行它。

ars1skjm

ars1skjm1#

检查是否已添加最新的firebase实时数据库依赖项-https://firebase.google.com/docs/android/setup

com.google.firebase:firebase-database:19.5.1

检查您的依赖项是否已同步。
检查您的 realtime databasepermissions 访问数据和 read/write 我不知道怎么做 authenticated .
通过检查 firebase console 尝试使缓存无效并启动android studio。

相关问题