Try to search your question here, if you can't find : Ask Any Question Now ?

"Make sure to call FirebaseApp.initializeApp(Context) first." How to fix this error?

HomeCategory: stackoverflow"Make sure to call FirebaseApp.initializeApp(Context) first." How to fix this error?
Avatarpragati asked 5 months ago

So Im trying to create an easy android App using Android Studio and when i build it seems fine but when i run the app crashes so i ve been told to check the Logcat and there it says that FirebaseApp is not initialized in my main activity. So i Added

FirebaseApp.initializeApp(this);

to onCreate() and it cannot resolve symbol FirebaseApp so what can i do to fix this?

Heres my Github with it:

https://github.com/radekkrowicki/Cv4

and theres my Mainactivity:

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.database.FirebaseListAdapter;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {
private static final int SIGN_IN_REQUEST_CODE = 111;
private FirebaseListAdapter<ChatMessage> adapter;
private ListView listView;
private String loggedInUserName = "";
private FirebaseAuth mAuth;


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

    //find views by Ids
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    final EditText input = (EditText) findViewById(R.id.input);
    listView = (ListView) findViewById(R.id.list);

    if (FirebaseAuth.getInstance().getCurrentUser() == null) {
        // Start sign in/sign up activity
        startActivityForResult(AuthUI.getInstance()
                .createSignInIntentBuilder()
                .build(), SIGN_IN_REQUEST_CODE);
    } else {
        // User is already signed in, show list of messages
        showAllOldMessages();
    }

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (input.getText().toString().trim().equals("")) {
                Toast.makeText(MainActivity.this, "Please enter some texts!", Toast.LENGTH_SHORT).show();
            } else {
                FirebaseDatabase.getInstance()
                        .getReference()
                        .push()
                        .setValue(new ChatMessage(input.getText().toString(),
                                FirebaseAuth.getInstance().getCurrentUser().getDisplayName(),
                                FirebaseAuth.getInstance().getCurrentUser().getUid())
                        );
                input.setText("");
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.menu_sign_out) {
        AuthUI.getInstance().signOut(this)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Toast.makeText(MainActivity.this, "You have logged out!", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                });
    }
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == SIGN_IN_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "Signed in successful!", Toast.LENGTH_LONG).show();
            showAllOldMessages();
        } else {
            Toast.makeText(this, "Sign in failed, please try again later", Toast.LENGTH_LONG).show();

            // Close the app
            finish();
        }
    }
}

private void showAllOldMessages() {
    loggedInUserName = FirebaseAuth.getInstance().getCurrentUser().getUid();
    Log.d("Main", "user id: " + loggedInUserName);

    adapter = new MessageAdapter(this, ChatMessage.class, R.layout.item_in_message,
            FirebaseDatabase.getInstance().getReference());
    listView.setAdapter(adapter);
}

public String getLoggedInUserName() {
    return loggedInUserName;
}
}

Thanks for the help in advance.

1 Answers
Best Answer
AvatarAmit answered 5 months ago
Your Answer

18 + 6 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com