gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:04:08 24 4
gpt4 key购买 nike

我花了大约三天的时间试图解决一个阻止我完成聊天应用程序的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com