2

My problem is quite a troublesome one, i made an app on Android Studio that has a TextView that displays the Firebase registration token upon launch (I'm still new to Firebase and Android Studio, i'm experimenting), i've been building APK files of my app to test it on my phone since Android Studio doesn't detect my phone when it's hooked to my laptop for some reason and i can't use emulation because my laptop is old (i think it doesn't support virtualization), so whenever i try to launch my app on my phone i get the equivalent of MyApp stopped working in French and i don't know how to view the crash log files.

tl;dr i wanna know what's causing my app to crash

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");
                      }
                    }
                });
    }

}

Edit: i managed to run my app on my phone through Android Studio by downloading the right drivers, i checked the logcat when i ran it and i got this:

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) 

Edit 2: I added the following line of code referred by user Riley Manda:

FirebaseApp.initializeApp(this);

in onCreate() method and i get this error now:

log trace:

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) 

Edit 3: I'm making another thread about this problem since it seems like the thread has drifted away from its original purpose, thanks everyone for answering.

Amine
  • 901
  • 2
  • 14
  • 33
  • 1
    Did you enable the developer settings on your phone? Did you install appropriate drivers (if on Windows)? You may also need to switch to "USB for file access" or similar for your phone to be recognized. It's impossible to give any feedback without seeing code or errors – derpirscher Mar 31 '19 at 17:07
  • yes i have it on the equivalent of media file transfer in French for USB access which is obviously the best choice, i enabled developer settings and USB debugging but Android Studio still doesn't detect, i'll check for drivers @derpirscher – Amine Mar 31 '19 at 17:10
  • PLease past your error messgae your code seem fine – amos godwin Mar 31 '19 at 17:12
  • @amosgodwin read the question... he said he can't connect the phone and does not have log trace – Masoom Badi Mar 31 '19 at 17:14
  • @Sam. He said He gets the equivalent of my apps not working in french – amos godwin Mar 31 '19 at 17:17
  • @amosgodwin Care to explain this _i've been building APK files of my app to test it on my phone since Android Studio doesn't detect my phone when it's hooked to my laptop for some reason and i can't use emulation because my laptop is old_ – Masoom Badi Mar 31 '19 at 17:19
  • @amosgodwin Yes my app crashes whenever i launch it on my phone and i have no access to the log trace – Amine Mar 31 '19 at 17:19
  • 1
    Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Mar 31 '19 at 17:54
  • Updated question with log trace – Amine Mar 31 '19 at 18:01

2 Answers2

1

Connect your app to Crashlytics,this way you will receive crash emails that will lead you to the exact issue thats causing your app to crash.

enter image description here

Follow the instructions on firebase to setup crashlytics for your app in Android Studio.And each time your app crashes, you will receive an email from Crashlytics:

enter image description here

This way, each time your app crashes from your physical devices notification email from Crashlytics is automatically sent to your email indicating the exact line of code/issue that's causing your app to crash.

Based on your crashLog:

You have to initialise firebase in your activity: Add this to your activity:

 FirebaseApp.initializeApp(this);

In your onCreate:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourlayout);
        FirebaseApp.initializeApp(this);
RileyManda
  • 2,536
  • 25
  • 30
  • 1
    Yes you're right, i installed the drivers now though and will soon have the log trace and will update my post as soon as it's done building (been doing so for 15 minutes) – Amine Mar 31 '19 at 17:50
  • Great.If my answer was helpful, please mark it as the correct answer. – RileyManda Mar 31 '19 at 17:53
  • 1
    In Addition,you can always enable "offline work"in your Android studio to speed up the build speeds.Check out this link:https://stackoverflow.com/questions/29391421/android-studio-gradle-takes-too-long-to-build link :for details on how to enable "offline work" – RileyManda Mar 31 '19 at 17:57
  • You're answer was helpful, thanks, but it wasn't the correct answer, i updated my question with the log trace though. – Amine Mar 31 '19 at 18:01
0

Add this FirebaseApp.initializeApp(this);just after setContentView(R.layout.yourlayout); or before you make any connections to firebase

Also for your device not showing in android studio

amos godwin
  • 930
  • 9
  • 12
  • Thanks amos for your answer, i already added FirebaseApp.initializeApp(this); in my onCreate() method just after setContentView(R.layout.yourlayout); as for my device connection i already solved that by downloading the appropriate drivers, thanks! – Amine Mar 31 '19 at 18:18