如何修复使用Firebase的Android应用崩溃?

时间:2019-03-31 17:00:56

标签: java android firebase firebase-cloud-messaging

我的问题很麻烦,我在Android Studio上制作了一个具有TextView的应用程序,该应用程序在启动时显示Firebase注册令牌(我对Firebase和Android Studio还是陌生的,我正在尝试),我一直在构建我的应用程序的APK文件以在手机上对其进行测试,因为由于某些原因,Android Studio在将其挂接到笔记本电脑时无法检测到我的手机,并且我无法使用仿真功能,因为我的笔记本电脑很旧(我认为它不是(不支持虚拟化),因此每当我尝试在手机上启动我的应用程序时,我都会得到相当于MyApp的法语停止工作,而且我不知道如何查看崩溃日志文件。

tl;博士,我想知道导致我的应用崩溃的原因

MainActivity.java

package com.gci.gestioncapteursincendie;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView= findViewById(R.id.textViewToken);
        FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {
                      if(task.isSuccessful()){
                          String token = task.getResult().getToken();
                          textView.setText("Token : " + token);
                        }
                        else
                      {
                          textView.setText("Token Not Generated");
                      }
                    }
                });
    }

}

编辑:通过下载正确的驱动程序,我设法通过Android Studio在手机上运行我的应用程序,我在运行logcat时检查了它,并得到了以下提示:

03-31 18:55:45.069 8855-8855/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.gci.gestioncapteursincendie, PID: 8855
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gci.gestioncapteursincendie/com.gci.gestioncapteursincendie.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.gci.gestioncapteursincendie. Make sure to call FirebaseApp.initializeApp(Context) first.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
        at android.app.ActivityThread.access$1100(ActivityThread.java:229)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7406)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.gci.gestioncapteursincendie. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
        at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
        at com.gci.gestioncapteursincendie.MainActivity.onCreate(MainActivity.java:38)
        at android.app.Activity.performCreate(Activity.java:6904)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:7406) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

编辑2: 我添加了用户Riley Manda引用的以下代码行:

FirebaseApp.initializeApp(this);

onCreate()方法中,我现在收到此错误:

日志跟踪:

03-31 19:10:19.203 11562-11562/com.gci.gestioncapteursincendie E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.gci.gestioncapteursincendie, PID: 11562
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gci.gestioncapteursincendie/com.gci.gestioncapteursincendie.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.gci.gestioncapteursincendie. Make sure to call FirebaseApp.initializeApp(Context) first.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
        at android.app.ActivityThread.access$1100(ActivityThread.java:229)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7406)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.gci.gestioncapteursincendie. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
        at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
        at com.gci.gestioncapteursincendie.MainActivity.onCreate(MainActivity.java:41)
        at android.app.Activity.performCreate(Activity.java:6904)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:7406) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

编辑3:由于该线程似乎已偏离其最初的目的,我正在就此问题进行另一次讨论,谢谢大家的回答。

2 个答案:

答案 0 :(得分:1)

将您的应用程序连接到Crashlytics,这样您将收到崩溃电子邮件,这些电子邮件将导致您导致导致应用程序崩溃的确切问题。

enter image description here

按照firebase上的说明在Android Studio中为您的应用程序设置crashlytics。每次您的应用程序崩溃时,您都会收到一封来自Crashlytics的电子邮件:

enter image description here

通过这种方式,每次您的应用在物理设备上崩溃时,来自Crashlytics的通知电子邮件都会自动发送到您的电子邮件中,指明导致应用崩溃的确切代码行/问题。

基于您的崩溃日志:

您必须在活动中初始化firebase: 将此添加到您的活动中:

 FirebaseApp.initializeApp(this);

在您的onCreate中:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourlayout);
        FirebaseApp.initializeApp(this);

答案 1 :(得分:0)

FirebaseApp.initializeApp(this);之后或在与Firebase建立任何连接之前添加此setContentView(R.layout.yourlayout);

您的设备也无法在android studio中显示