java-“确保首先调用FirebaseApp.initializeApp(Context).”如何解决此错误?

java-“确保首先调用FirebaseApp.initializeApp(Context).”如何解决此错误?,第1张

概述因此,我尝试使用AndroidStudio创建一个简单的android应用,当我构建它时看起来不错,但是当我运行该应用时发生崩溃,因此我被告知检查Logcat,并且那里说FirebaseApp未在我的主设备中初始化活动.所以我加了FirebaseApp.initializeApp(this);到onCreate()并且无法解析符号Firebase

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

FirebaseApp.initializeApp(this);

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

这是我的Github:

https://github.com/radekkrowicki/Cv4

还有我的主要活动:

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;@OverrIDeprotected 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("");            }        }    });}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.main, menu);    return true;}@OverrIDepublic 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;}@OverrIDeprotected 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;}}

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

解决方法:

刚刚克隆了您的仓库,除了这一行外,一切似乎都很好

classpath’com.Google.gms:Google-services:4.1.0′

在项目级别的gradle文件中.

似乎该版本中的错误已将其升级到

classpath’com.Google.gms:Google-services:4.2.0′

而且一切正常.尝试一下,让我知道您是否遇到任何问题.

总结

以上是内存溢出为你收集整理的java-“确保首先调用FirebaseApp.initializeApp(Context).”如何解决此错误?全部内容,希望文章能够帮你解决java-“确保首先调用FirebaseApp.initializeApp(Context).”如何解决此错误?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1081724.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存