我试图在应用程序中使用Firebase从我的设备中检索注册令牌,该应用程序具有TextView,目的是在每次获取java.lang时运行该应用程序时在其中包含令牌的值.RuntimeException错误,我按照指示添加了此代码mockito
,但仍然收到此错误,我对Android Studio和Firebase还是很陌生,并且仍在尝试。
MainActivity.java
FirebaseApp.initializeApp(this);
日志跟踪:
package com.gci.gestioncapteursincendie;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseApp.initializeApp(this);
textView= findViewById(R.id.textViewToken);
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if(task.isSuccessful()){
String token = task.getResult().getToken();
textView.setText("Token : " + token);
}
else
{
textView.setText("Token Not Generated");
}
}
});
}
}
build.gradle
03-31 19:10:19.203 11562-11562/com.gci.gestioncapteursincendie E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gci.gestioncapteursincendie, PID: 11562
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gci.gestioncapteursincendie/com.gci.gestioncapteursincendie.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.gci.gestioncapteursincendie. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.gci.gestioncapteursincendie. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
at com.gci.gestioncapteursincendie.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7406)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:1)
从文档中
对于绝大多数应用程序,
from urllib.parse import urlunparse urlunparse(('https', 'httpbin.org', '/get', None, None, None)) # 'https://httpbin.org/get'
会通过应用程序FirebaseInitProvider
文件中包含的数据来处理Firebase为其配置为可处理的默认项目的初始化。如果某个应用除了默认项目之外还需要访问另一个Firebase项目,则必须使用
google-services.json
以编程方式创建该关系。
如果您在两个不同的项目中使用此应用程序,则需要使用initializeApp(Context, FirebaseOptions, String)
。
https://firebase.google.com/docs/reference/android/com/google/firebase/FirebaseApp
也将您的Google服务版本更新为initializeApp()
,因为4.2.0
遇到配置问题。
4.1.0
答案 1 :(得分:1)
在应用程序gradle的末尾添加应用插件:“ com.google.gms.google-services”:
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.google.gms:google-services:4.2.0'
...
}
apply plugin: 'com.google.gms.google-services'