我是靠谱客的博主 外向大雁,最近开发中收集的这篇文章主要介绍java initialize_java-“确保首先调用FirebaseApp.initializeApp(Con...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

因此,我尝试使用Android Studio创建一个简单的android应用,当我构建它时看起来不错,但是当我运行该应用时发生崩溃,因此我被告知检查Logcat,并且那里说FirebaseApp未在我的主设备中初始化活动.所以我加了

FirebaseApp.initializeApp(this);

到onCreate()并且无法解析符号FirebaseApp,那么该怎么办才能解决此问题?

这是我的Github:

还有我的主要活动:

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 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() {

@Override

public void onComplete(@NonNull Task 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;

}

}

我在这里先向您的帮助表示感谢.

最后

以上就是外向大雁为你收集整理的java initialize_java-“确保首先调用FirebaseApp.initializeApp(Con...的全部内容,希望文章能够帮你解决java initialize_java-“确保首先调用FirebaseApp.initializeApp(Con...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部