0

当我添加这个

        demoRef = FirebaseDatabase.getInstance().getReference().child("txt");

我的应用程序崩溃,当我删除它时,应用程序工作可以有人帮助我。

我尝试了很多方法,比如更改数据库的版本,但没有任何效果,我花了 3 多周的时间没有任何解决

这是我的主要活动

公共类 MainActivity 扩展 Activity {

private HashMap <String,Object>map=new HashMap();
private Button send;
private EditText edit;
private DatabaseReference demoRef;

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

    FirebaseApp.initializeApp(this);
    demoRef = FirebaseDatabase.getInstance().getReference().child("txt");

    send=findViewById(R.id.mainButton1);
    edit=findViewById(R.id.mainEditText1);


}

}

这是我的构建

dependencies {
    //compile 'com.google.firebase:firebase-database:+'

    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.google.firebase:firebase-database:16.0.1'
    compile 'com.google.firebase:firebase-core:16.0.1'



}
apply plugin: 'com.google.gms.google-services'

这是我的 Logcat

FATAL EXCEPTION: main
Process: com.firebase, PID: 5291
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firebase/com.firebase.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(SourceFile:218)
at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
at com.firebase.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6677)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
... 9 more
Shutting down VM
FATAL EXCEPTION: main
Process: com.firebase, PID: 8039
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firebase/com.firebase.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(SourceFile:218)
at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
at com.firebase.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6677)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
... 9 more
4

2 回答 2

0

FirebaseApp.initializeApp(this);从应用程序类的 onCreate 中删除MainActivity并将其放入Application看这里

顺便问一下,您正在测试的 API LEVEL 是什么?从Marshmallow开始,您需要为您的应用添加运行时权限。并且您可能需要使用本地数据库的WRITE_STORAGE权限。

希望这可以帮助!

于 2019-02-06T16:31:26.960 回答
0

消除

FirebaseApp.initializeApp(this);

让参考像这样

private HashMap <String,Object>map=new HashMap();
private Button send;
private EditText edit;
private DatabaseReference demoRef;

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

    demoRef = FirebaseDatabase.getInstance().getReference().child("txt");

    send=findViewById(R.id.mainButton1);
    edit=findViewById(R.id.mainEditText1);


}

并确保您在清单中具有 INTERNET 权限

于 2019-02-06T16:11:35.247 回答