如何解决Android Firebase错误“缺省的FirebaseApp在此过程中未初始化”? [英] How can I solve the Android Firebase error "Default FirebaseApp is not initialised in this process"?

查看:2091
本文介绍了如何解决Android Firebase错误“缺省的FirebaseApp在此过程中未初始化”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FirebaseAuth进行电子邮件和密码的用户注册,并且已经在我的项目中添加了插件和依赖项。
$ b MainActivity.java

  public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText ed_email,ed_pa​​ss;
Button but_login;
ProgressDialog progressDialog;
FirebaseAuth firebaseAuth;
上下文上下文;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
FirebaseApp.initializeApp(context);
firebaseAuth = FirebaseAuth.getInstance();

ed_email =(EditText)findViewById(R.id.ed_email);
ed_pa​​ss =(EditText)findViewById(R.id.ed_pa​​ss);
but_login =(Button)findViewById(R.id.but_login);
but_login.setOnClickListener(this);
progressDialog = new ProgressDialog(this);

public void registerUser(){
String email = ed_email.getText()。toString()。trim();
String pass = ed_pa​​ss.getText()。toString()。trim();
if(TextUtils.isEmpty(email)){
Toast.makeText(getApplicationContext(),Invalid Input,Toast.LENGTH_SHORT).show();
return;

if(TextUtils.isEmpty(pass)){
Toast.makeText(getApplicationContext(),Invalid Input,Toast.LENGTH_SHORT).show();
return;
}
progressDialog.setMessage(您正在注册...);
progressDialog.show();
firebaseAuth.createUserWithEmailAndPassword(email,pass).addOnCompleteListener(this,new OnCompleteListener< AuthResult>(){
@Override $ b $ public void onComplete(@NonNull Task< AuthResult> task){
if(task.isSuccessful()){
Toast.makeText(getApplicationContext(),Success,Toast.LENGTH_SHORT).show();
progressDialog.hide();
} else {
Toast.makeText(getApplicationContext(),Sorry ... !!!,Toast.LENGTH_SHORT).show();
progressDialog.hide();
}
}

});

$ b}

@Override
public void onClick(View v){
registerUser();

code










$ b $ p


$ b

logcat

  com.skapsdevelopment.firebase E / AndroidRuntime:FATAL EXCEPTION:main 
过程:com.skapsdevelopment.firebase,PID:31993
主题:主题:{default = overlay:com.resurrectionremix.pitchblack,iconPack:com.baranovgroup.nstyle,fontPkg:com.resurrectionremix.pitchblack,com.android。 systemui = overlay:com.resurrectionremix.pitchblack,com.android.systemui.navbar = overlay:com.resurrectionremix.pitchblack}
java.lang.RuntimeException:无法启动活动ComponentInfo {com.skapsdevelopment.firebase / com。 skapsdevelopment.firebase.MainActivity}:java.lang.IllegalStateException:默认的FirebaseApp在这个过程中没有初始化com.skapsdevelopment.firebase。确保先调用FirebaseApp.initializeApp(Context)。
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.- wrap11(ActivityThread.java)
at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
在java.lang.reflect.Method.invoke(Native方法)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
导致:java.lang.IllegalStateException:缺省的FirebaseApp在这个过程中没有被初始化com.skapsdevelopment.firebase。确保先调用FirebaseApp.initializeApp(Context)。
在com.google.firebase.FirebaseApp.getInstance(未知来源)
在com.google.firebase.auth.FirebaseAuth.getInstance(未知来源)
在com.skapsdevelopment.firebase.MainActivity .onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread .java)
at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android。 os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
在com.android.internal.os.Zygot eInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的应用程序没有启动并显示以下错误:

  java .lang.IllegalStateException:在此
进程com.skapsdevelopment.firebase中,未初始化FirebaseApp。
确保先调用FirebaseApp.initializeApp(Context)。

为什么应用程式无法正常启动?



在您的项目级别添加gradle文件:

  buildscript {
// ...
依赖关系{
// ...
classpath'com.google.gms:google-services:3.0.0'
}
}

在你的app-build gradle文件中:

  apply plugin:'com.android.application'

android {
// ...
}

依赖关系{
// ...
compile'c​​om。 google.firebase:firebase-core:10.0.1'

//获取找不到错误?确保你有
// Android SDK管理器中最新的Google Repository

$ b $ //在底部添加
apply插件:com.google .gms.google-services'

链接


I am using FirebaseAuth for user registration with email and password, and I have already added the plugin and dependencies in my project.

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText ed_email, ed_pass;
Button but_login;
ProgressDialog progressDialog;
FirebaseAuth firebaseAuth;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=getApplicationContext();
    FirebaseApp.initializeApp(context);
    firebaseAuth=FirebaseAuth.getInstance();

    ed_email= (EditText) findViewById(R.id.ed_email);
    ed_pass= (EditText) findViewById(R.id.ed_pass);
    but_login= (Button) findViewById(R.id.but_login);
    but_login.setOnClickListener(this);
    progressDialog=new ProgressDialog(this);
}
public void registerUser(){
    String email=ed_email.getText().toString().trim();
    String pass=ed_pass.getText().toString().trim();
    if(TextUtils.isEmpty(email)){
        Toast.makeText(getApplicationContext(),"Invalid Input",Toast.LENGTH_SHORT).show();
        return;
    }
    if(TextUtils.isEmpty(pass)){
        Toast.makeText(getApplicationContext(),"Invalid Input",Toast.LENGTH_SHORT).show();
        return;
    }
    progressDialog.setMessage("You are registering...");
    progressDialog.show();
    firebaseAuth.createUserWithEmailAndPassword(email,pass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                progressDialog.hide();
            } else {
                Toast.makeText(getApplicationContext(), "Sorry...!!!", Toast.LENGTH_SHORT).show();
                progressDialog.hide();
            }
        }

    });


}

@Override
public void onClick(View v) {
    registerUser();
}

}

logcat -

com.skapsdevelopment.firebase E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.skapsdevelopment.firebase, PID: 31993
Theme: themes:{default=overlay:com.resurrectionremix.pitchblack, iconPack:com.baranovgroup.nstyle, fontPkg:com.resurrectionremix.pitchblack, com.android.systemui=overlay:com.resurrectionremix.pitchblack, com.android.systemui.navbar=overlay:com.resurrectionremix.pitchblack}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skapsdevelopment.firebase/com.skapsdevelopment.firebase.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.skapsdevelopment.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
   at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5461)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.skapsdevelopment.firebase. Make sure to call FirebaseApp.initializeApp(Context) first.
   at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
   at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source)
   at com.skapsdevelopment.firebase.MainActivity.onCreate(MainActivity.java:35)
   at android.app.Activity.performCreate(Activity.java:6251)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5461) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

My App is not starting and showing the following error:

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this 
process com.skapsdevelopment.firebase. 
Make sure to call FirebaseApp.initializeApp(Context) first.

Why is the app not starting properly?

解决方案

Double-check the following as this has worked for me:

Added in your project level gradle file:

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

And on your app-build gradle file:

apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  compile 'com.google.firebase:firebase-core:10.0.1'

  // Getting a "Could not find" error? Make sure you have
  // the latest Google Repository in the Android SDK manager
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services' 

Link

这篇关于如何解决Android Firebase错误“缺省的FirebaseApp在此过程中未初始化”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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