FirebaseApp.initializeApp(Context); Android错误; [英] FirebaseApp.initializeApp(Context); Android error;

查看:258
本文介绍了FirebaseApp.initializeApp(Context); Android错误;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经花了大约三天的时间来解决一个Java错误,该错误使我无法完成聊天应用程序!当然,这是一个firebase错误!我是Android开发的新手,所以当您回答时,如果您可以使用简单的术语进行解释,将会大有帮助! -

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)

我针对该问题的实际代码如下(第51和52行):

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

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

看起来应该可以,但是Context出现错误!这是旧的expression expected错误,我已经尽力解决了. 我已经尝试了以下内容以及更多内容:

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)

没有任何效果!我唯一能找到的就是(Context),除了Context ... UGH上有错误.

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

我尝试在MainActivity和ChatActivity中都调用它,但是两者都出错.请帮帮我!

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

这些是我的依赖项:

  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

示例:

    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.

谢谢!

ChatActivity onCreate方法代码:

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);

}

谢谢!尝试修复注释尝试后,此代码进行了更改,尽管没有一个可以像以前所说的那样在任何时候都起作用,就像我说的那样,我放了(this)或不是(Context)的任何东西,它只是要求输入(Context).

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.

编辑包含清单文件:

<?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>

谢谢!

推荐答案

添加:

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

在您的应用gradle文件中

in your app gradle file

根据文档:

如文档所述:

任何FirebaseApp初始化必须仅在应用程序的主要过程中发生.不支持在主流程以外的其他流程中使用Firebase,这可能会导致与资源争用相关的问题.

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"
       

然后执行以下操作:

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.

这篇关于FirebaseApp.initializeApp(Context); Android错误;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆