为什么我的ValueEventListener无法正常工作?

时间:2018-09-09 08:18:41

标签: android firebase firebase-realtime-database

我正在尝试从Firebase实时数据库中读取值,但是没有任何反应。我找不到我的代码出了什么问题。我已经相应地设定了规则。以下是MainActivity.java

中的代码
package com.borah.manjit.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

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;


public class MainActivity extends AppCompatActivity {
    String TAG="mn";
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference().child("Games");
        tv=findViewById(R.id.tv);

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String s=dataSnapshot.child("game1").getValue(String.class);
                tv.setText(s);
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Here is the data in my database

数据库的规则设置为以下

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

是否将textview的文本设置为“ C”?

这是我的日志的样子

  

09-10 13:34:52.854 23293-23293 / com.borah.manjit.myapplication D / mn:   https://myapplication6-55473.firebaseio.com/Games 09-10 13:34:59.184   23293-23293 / com.borah.manjit.myapplication W / mn:无法读取值。       com.google.firebase.database.DatabaseException:Firebase数据库错误:权限被拒绝           位于com.google.firebase.database.DatabaseError.toException(未知来源)           在com.borah.manjit.myapplication.MainActivity $ 1.onCancelled(MainActivity.java:40)           在com.google.android.gms.internal.zzegf.zza(未知来源)           com.google.android.gms.internal.zzehy.zzbyc(未知来源)           com.google.android.gms.internal.zzeig.run(未知来源)           在android.os.Handler.handleCallback(Handler.java:739)           在android.os.Handler.dispatchMessage(Handler.java:95)           在android.os.Looper.loop(Looper.java:159)           在android.app.ActivityThread.main(ActivityThread.java:5461)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:964)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

0 个答案:

没有答案