https://github.com/arnesson/cordova-plugin-firebase/issues/142
ратил около трех дней, пытаясь устранить ошибку Java, которая мешает мне завершить работу в приложении чата! И, конечно, это ошибка базы данных! Я новичок в разработке 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
получить ошибку! Это старыйexpression expected
ошибка, и я попробовал все, чтобы это исправить. я попробовал следующее плюс еще немного:
FirebaseApp.initializeApp(this)
FirebaseApp.initializeApp(this.context)
FirebaseApp.initializeApp(context)
FirebaseApp.initializeApp(many other things)
И ничего не работает! Единственное, что я могу найти для работы, это просто(Context)
кроме того, что есть ошибка в контексте ... UGH.
Я пытался вызвать его как в 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)
вместо.
Изменить, включая файл манифеста:
<?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>
Спасибо!