无法在Firebase

时间:2017-02-20 12:37:01

标签: java android firebase firebase-realtime-database

我只是想从Firebase实时数据库中检索JSON数据并将其显示在文本视图中

并且我不确定为什么会发生这种情况,添加了gms,在gradle中应用了插件项目级别和模块级别,这是我的MainActivity.java 很抱歉将此添加到JSSnippet

任何帮助将不胜感激



package com.example.halo.firebase;

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

import com.google.firebase.FirebaseApp;
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 {
    private TextView MagTxt;
    private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    private DatabaseReference mRootreference = firebaseDatabase.getReference();
    private DatabaseReference mChildreference = mRootreference.child("message");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //FirebaseApp.initializeApp(this);
        setContentView(R.layout.activity_main);
        MagTxt = (TextView) findViewById(R.id.msgTxt);
        MagTxt.setText("Refreshing Data...");



    }

    @Override
    protected void onStart() {
        super.onStart();
    mChildreference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
       String Message = dataSnapshot.getValue(String.class);
        MagTxt.setText(Message);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    }
}






FATAL EXCEPTION: main
                                                                           Process: com.example.halo.firebase, PID: 18428
                                                                           java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.halo.firebase/com.example.halo.firebase.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.halo.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6126)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                            Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.halo.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                                               at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                               at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                                                                               at com.example.halo.firebase.MainActivity.<init>(MainActivity.java:16)
                                                                               at java.lang.Class.newInstance(Native Method)
                                                                               at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6126) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

我认为问题在于你在公开场合实例化了FirebaseDatabaseDatabaseReference

您应该创建变量,然后在onCreate

中实例化它们

像这样:

public class MainActivity extends AppCompatActivity {

    // without instantiation

    private TextView MagTxt;
    private FirebaseDatabase firebaseDatabase;
    private DatabaseReference mRootreference;
    private DatabaseReference mChildreference;

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

        // instantiate in the onCreate method

        firebaseDatabase = FirebaseDatabase.getInstance();
        mRootreference = firebaseDatabase.getReference();
        mChildreference = mRootreference.child("message");

        MagTxt = (TextView) findViewById(R.id.msgTxt);
        MagTxt.setText("Refreshing Data...");
    }

    @Override
    protected void onStart() {
        super.onStart();
    mChildreference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
       String Message = dataSnapshot.getValue(String.class);
        MagTxt.setText(Message);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    }
}

希望有所帮助。