java - FirebaseApp.initializeApp(Context);安卓错误;

标签 java android firebase firebase-realtime-database

我花了大约三天的时间试图解决一个阻止我完成聊天应用程序的 java 错误!当然,这是一个 firebase 错误!我是 Android 开发新手,所以当您回答时,如果您能用简单的术语进行解释,将会有很大帮助! -

    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 行):

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

看起来应该可以工作,但是 Context 出现错误!这是旧的 expected expression 错误,我已尝试一切方法来修复它。 我已经尝试了以下方法以及更多方法:

FirebaseApp.initializeApp(this)

FirebaseApp.initializeApp(this.context)

FirebaseApp.initializeApp(context)

FirebaseApp.initializeApp(many other things)

没有任何作用!我唯一能找到的工作就是 (Context) ,除了 Context 上有一个错误......呃。

我尝试在 MainActivity 和 ChatActivity 中调用它,但两者都出现错误。请帮助我!

这些是我的依赖项:

  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'

}

我尝试使用有效的实际代码

示例:

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

但即使它没有说有任何错误,当我运行我的应用程序时,它会立即关闭并弹出相同的错误。

谢谢!

编辑:

ChatActivity onCreate方法代码:

  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)

编辑 list 文件:

<?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 文件中

根据文档:

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.

您需要初始化它,而不是在 Activity 中。

将应用程序类添加到 list 示例中:

      <applicaton
       android:name="MyApplication"
       

然后这样做:

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

并从 Activity 中删除初始化。您需要在作为基类的应用程序类中对其进行初始化。

关于java - FirebaseApp.initializeApp(Context);安卓错误;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47643654/

相关文章:

typescript - Firebase云函数-触发Expo SDK向用户推送通知

reactjs - Firebase grecaptcha 未定义 react

java - 生成的 android 项目中缺少 gradlew

java - 通过Spring框架获取依赖图(逆图)

java - 创建类似于 Twitter 应用程序和其他应用程序的登录页面

android - 为什么 webkit 浏览器会缩小手机上的一些 float 元素?

ios - tableview 单元格内的类似 Firebase 的系统

java - 是否存在这样的 JSON 字符串生成器?

java - 是否有适用于 Torque/PBS 的 DRMAA Java 库?

android - 如何减少 Android 布局中文本之间的行距?