0

When I add this

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

My App crash and when I delete it the app work can someone help me .

I tried many ways like change the version of database but nothing work and it take me more than 3 weeks without any solve

This is my Mainactivity

public class MainActivity extends 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);


}

}

This is my build

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'

This is my 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
Lakshan Dissanayake
  • 521
  • 1
  • 4
  • 18
user11019720
  • 43
  • 1
  • 4

2 Answers2

0

Remove

FirebaseApp.initializeApp(this);

and just let the reference like 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);


}

And make sure you have the INTERNET permission in your manifest

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0

remove FirebaseApp.initializeApp(this); from MainActivity and put it in onCreate of your app's Application class. Look here.

By the way, What is the API LEVEL you're testing? From Marshmallow onwards you'll need to add runtime permissions to your app. and you probably need WRITE_STORAGE permission to use the local database.

Hope this helps!

Lakshan Dissanayake
  • 521
  • 1
  • 4
  • 18