Teo
Teo

Reputation: 137

FirebaseApp.initializeApp(Context); Android error;

I've spent about three days now trying to solve a java error that's stopping me from finishing my chat app! And of course it's a firebase error! I'm new to Android Developing so when you answer, if you could explain it using simple terms, it'll help a lot! -

    Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.teo.myapplication. 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.teo.myapplication.MainActivity.onCreate(MainActivity.java:52)

my actual code for the problem looks like this (lines 51 and 52):

    FirebaseApp.initializeApp(Context);
    dataBase = FirebaseDatabase.getInstance();

It looks like it should work, but Context get's an error! It's the old expression expected error and I've tried everything to fix it. i've tried the following plus some more:

FirebaseApp.initializeApp(this)

FirebaseApp.initializeApp(this.context)

FirebaseApp.initializeApp(context)

FirebaseApp.initializeApp(many other things)

And nothing works! The only thing I can find to work is just (Context) except there's an error on Context... UGH.

I've tried calling it in both MainActivity and ChatActivity but there's an error for both. Please help me!

these are my dependencies:

  dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:25.3.1'
    compile 'com.google.firebase:firebase-storage:10.2.0'
    compile 'com.google.firebase:firebase-auth:10.2.0'
    compile 'com.google.firebase:firebase-core:10.2.1'
    compile 'com.google.firebase:firebase-database:10.2.0'
    compile 'com.google.android.gms:play-services-ads:11.0.4'
    compile 'com.google.code.gson:gson:2.8.1'
    compile 'com.google.firebase:firebase-messaging:11.0.4'
    compile 'com.firebase:firebase-client-android:2.5.0'





    testCompile 'junit:junit:4.12'

}

I've tried to use the actual code that works

example:

    FirebaseApp.initializeApp(this);
  dataBase = FirebaseDatabase.getInstance();

But even though it doesn't say there's any errors, when I run my app, it closes right away and the same error pops up.

Thanks!

EDIT:

ChatActivity onCreate method code:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat_activity);




    Intent intent = getIntent();
    username = intent.getStringExtra("username");
    chatroomName = intent.getStringExtra("Chat_room_name");
    Incognito = intent.getBooleanExtra("Incognito", false);



    listview = (ListView) findViewById(R.id.list_view);
    chatsend = (EditText) findViewById(R.id.chat_function);
    final ArrayList<String> listArray = new ArrayList<String>();
    MyListAdapter adapter = new MyListAdapter(getApplicationContext(), listArray, ChatActivity.this);
    listview.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    context = this;
    FirebaseApp.initializeApp(this);
  dataBase = FirebaseDatabase.getInstance();
     ref = dataBase.getReference();

    // listArray.add(chatsend);

}

Thanks! This code was altered after trying to fix a attempts from comments although none worked as whenever, like said before, I put (this) or anything that isn't (Context), it just asks for (Context) instead.

Edit including the manifest File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.teo.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ChatActivity"/>
    </application>

</manifest>

Thanks!

Upvotes: 1

Views: 4456

Answers (3)

Ankit Maheshwari
Ankit Maheshwari

Reputation: 1680

I had this issue. I built the generated android project in the platform in android studio then I found the issue and solved this.

Follow this steps.

Inside the platforms folder, android/src/{package name}/MainActivity.java

Import the firebase library, * import com.google.firebase.FirebaseApp;

Inside the oncreate method, below super.oncreate(savedinstancestate),

Add FirebaseApp.initializeApp(this);

Last step,

Inside the platforms folder, android/res/values/string.xml,

Add
***

*** can be found in the googleservice.json file
"client": [
{
"client_info": {
"mobilesdk_app_id": this value


Ref (for more detail): https://github.com/arnesson/cordova-plugin-firebase/issues/142

Upvotes: 0

Bob Snyder
Bob Snyder

Reputation: 38289

You are using both the current Firebase SDK and the legacy SDK, com.firebase:firebase-client-android:2.5.0, which has been deprecated for more than a year. For new development, there is no reason to use the legacy SDK. Follow the steps the Upgrade Guide to eliminate use of the legacy SDK.

It's also important to use the same version number for all Firebase and Google Play libraries. You have 10.2.0, 10.2.1 and 11.0.4.

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80904

Add:

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

in your app gradle file

According to the docs:

As said in the docs:

Any FirebaseApp initialization must occur only in the main process of the app. Use of Firebase in processes other than the main process is not supported and will likely cause problems related to resource contention.

you need to initialize it not in the activity.

add an application class to your manifest example:

      <applicaton
       android:name="MyApplication"
       

then do this:

public class MyApplication extends Application {
  @Override
public void onCreate() {
    super.onCreate();
   FirebaseApp.initializeApp(this);
}

and remove the initialization from the activity. You need to initialize it in the application class which is the base class.

Upvotes: 4

Related Questions