尽管在Application类中调用了FirebaseApp.initializeApp(),FirebaseApp仍未初始化 [英] FirebaseApp not initializing despite FirebaseApp.initializeApp() being called in Application class

查看:3231
本文介绍了尽管在Application类中调用了FirebaseApp.initializeApp(),FirebaseApp仍未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Android应用程序,我正在尝试使用Firebase实现用户身份验证。据我所知,我的应用程序已连接到我的Firebase服务器。



尝试从SignIn活动切换到SignUp活动时遇到运行时错误按钮按下。据我所知,运行时错误来自注册活动的 onCreate()

/ code>当我尝试用 FirebaseAuth.getInstance()初始化一个 FirebaseAuth 对象时调用。由于

  java.lang.IllegalStateException:这个调用失败:在这个过程中,缺省的firebaseapp没有初始化seniordesign.phoneafriend。确保调用FirebaseApp.initializeApp(Context)。 

然而,我在我的Application类中调用了这个调用: onCreate / code>方法,我认为会很好。我添加了 initalizeApp()调用给SignUp的 onCreate()调用,但没有骰子。我已经找了这个问题的其他人,但还没有找到类似的东西。感谢您的帮助。

AndroidManifest.xml

 < xml version =1.0encoding =utf-8?> 
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =seniordesign.phoneafriend>
< uses-permission android:name =android.permission.INTERNET/>
android:allowBackup =true
android:icon =@ mipmap / ic_launcher
android:label =@ string / app_name
android:supportsRtl =true
android:theme =@ style / AppTheme
android:name =seniordesign.phoneafriend.PhoneAFriend>
< activity android:name =。SignIn>
< intent-filter>

< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< activity android:name =。SignUp>< / activity>
< / application>

< / manifest>

PhoneAFriend.java(My Application类)

<公共类PhoneAFriend扩展应用程序{

public void onCreate(){
super.onCreate();
Firebase.setAndroidContext(this);
FirebaseApp.initializeApp(this);




$ b SignUp.java

  public class SignUp extends AppCompatActivity {
protected Firebase ref;
保护EditText emailText;
protected EditText passText;
保护EditText confirmText;
保护按钮按钮;
保护SignUp thisContext;

保护FirebaseAuth auth;
protected FirebaseAuth.AuthStateListener authListener;
私人View.OnClickListener onClickListener;

public static Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
ref =新的Firebase(https://phoneafriend-7fb6b.firebaseio.com);
emailText =(EditText)findViewById(R.id.signup_emailText);
passText =(EditText)findViewById(R.id.signup_passwordText);
confirmText =(EditText)findViewById(R.id.signup_passwordConfirm);
intent = new Intent(this,SignIn.class);
//尝试了这个
//FirebaseApp.initializeApp(this);
auth = FirebaseAuth.getInstance();


button =(Button)findViewById(R.id.signup_button);
onClickListener = new View.OnClickListener(){
@Override
public void onClick(View view){
createUser(view);
Log.v(SignUp Button,Clicked; Attempting to create user);
}
};
button.setOnClickListener(onClickListener);
$ b authListener = new FirebaseAuth.AuthStateListener(){
@Override $ b $ public void onAuthStateChanged(FirebaseAuth firebaseAuth){
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user!= null){
//用户登录$ b $ Log.d(FirebaseAuth,onAuthStateChanged:signed_in:+ user.getUid());
} else {
//用户签出
Log.d(FirebaseAuth,onAuthStateChanged:signed_out);
}
// ...
}
};
thisContext = this;

$ b @Override
public void onStart(){
super.onStart();
//auth.addAuthStateListener(authListener);


@Override
public void onStop(){
super.onStop();
if(authListener!= null){
//auth.removeAuthStateListener(authListener);



protected void createUser(View view){
String cString = null;
String pString = null;
String eString = emailText.getText()。toString();
if(passText.getText()!= null&&&; confirmText.getText()!= null){
pString = passText.getText()。toString();
cString = confirmText.getText()。toString();
Log.v(Signup:Pass Null check,Pass);
if(emailText.getText()!= null&& pString.equals(cString)&& passText.getText()!= null){
Log.v(Signup:Sign通过检查,通行证);
auth.createUserWithEmailAndPassword(emailText.getText()。toString(),passText.getText()。toString())
.addOnCompleteListener(this,new OnCompleteListener< AuthResult>(){
@覆盖
public void onComplete(@NonNull Task< AuthResult> task){
Log.v(createUser complete,status:+ task.isSuccessful());
if .isSuccessful()){
startActivity(SignUp.intent);
}
}

});

}
}

return;


$ / code $ / pre

解决方案

Just正如qbix所说的,你可以使用你打算使用的任何版本的API调用。如果可能的话,你应该使用新的API,因为它将来会得到更多的支持。



请参阅文档:

https://firebase.google.com/docs/database/ android / start /

删除:

  Firebase .setAndroidContext(本); 
FirebaseApp.initializeApp(this);





  FirebaseDatabase database = FirebaseDatabase.getInstance(); 

如果qbix将他的评论置于一个答案中,您应该接受他而不是我的看法,我几分钟。



另外: 是使用旧的firebase和需要帮助切换,本指南是现货,并将帮助您的开关。这是一个相当简单的开关。



https://firebase.google.com/support/guides/firebase-android


I am creating an Android application and I'm currently trying to implement user authentication using Firebase. As far as I can tell, my app is connected to my Firebase server.

I encounter a runtime error when attempting to switch from the SignIn activity to the SignUp activity via a button press. The app crashes and I encounter a runtime error.

So far as I can tell, the runtime error is from the SignUp activity's onCreate() call when I attempt to initialize a FirebaseAuth object with FirebaseAuth.getInstance(). This call fails due to

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process seniordesign.phoneafriend. Make sure to call FirebaseApp.initializeApp(Context).

However, I make this call in my Application class' onCreate() method which I thought would be fine. I added the initalizeApp() call to the SignUp's onCreate() call but no dice. I've looked for others with this issue but have not found anything similar. Thanks for any help.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="seniordesign.phoneafriend">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name="seniordesign.phoneafriend.PhoneAFriend">
        <activity android:name=".SignIn">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SignUp"></activity>
    </application>

</manifest>

PhoneAFriend.java (My Application class)

public class PhoneAFriend extends Application {

    public void onCreate(){
        super.onCreate();
        Firebase.setAndroidContext(this);
        FirebaseApp.initializeApp(this);
    }
}

SignUp.java

public class SignUp extends AppCompatActivity {
    protected Firebase ref;
    protected EditText emailText;
    protected EditText passText;
    protected EditText confirmText;
    protected Button button;
    protected SignUp thisContext;

    protected FirebaseAuth auth;
    protected FirebaseAuth.AuthStateListener authListener;
    private View.OnClickListener onClickListener;

    public static Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        ref = new Firebase("https://phoneafriend-7fb6b.firebaseio.com");
        emailText = (EditText) findViewById(R.id.signup_emailText);
        passText = (EditText) findViewById(R.id.signup_passwordText);
        confirmText = (EditText) findViewById(R.id.signup_passwordConfirm);
        intent = new Intent(this, SignIn.class);
        //Tried this already
        //FirebaseApp.initializeApp(this);
        auth = FirebaseAuth.getInstance();


        button = (Button) findViewById(R.id.signup_button);
        onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createUser(view);
                Log.v("SignUp Button" , "Clicked; Attempting to create user");
            }
        };
        button.setOnClickListener(onClickListener);

        authListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged( FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d("FirebaseAuth", "onAuthStateChanged:signed_in:" + user.getUid());
                } else {
                    // User is signed out
                    Log.d("FirebaseAuth", "onAuthStateChanged:signed_out");
                }
                // ...
            }
        };
        thisContext = this;

    }
    @Override
    public void onStart(){
        super.onStart();
        //auth.addAuthStateListener(authListener);
    }

    @Override
    public void onStop(){
        super.onStop();
        if(authListener != null) {
            //auth.removeAuthStateListener(authListener);
        }
    }

    protected void createUser(View view){
        String cString = null;
        String pString = null;
        String eString  = emailText.getText().toString();
        if(passText.getText() != null && confirmText.getText() != null) {
            pString = passText.getText().toString();
            cString = confirmText.getText().toString();
            Log.v("SignUP: Pass Null check" , "Pass" );
            if (emailText.getText() != null && pString.equals(cString) && passText.getText() != null) {
                Log.v("SignUP: Sign up check " , "Pass");
                auth.createUserWithEmailAndPassword(emailText.getText().toString() , passText.getText().toString())
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                Log.v("createUser complete" , "status: " + task.isSuccessful());
                                if(task.isSuccessful()){
                                    startActivity(SignUp.intent);
                                }
                            }

                        });

            }
        }

        return;
    }
}

解决方案

Just as qbix stated, you much use the API calls from whichever version you are going to use. If possible, you should use the newer API because it will definitely be supported much further into the future.

See the docs here:

https://firebase.google.com/docs/database/android/start/

Remove:

Firebase.setAndroidContext(this);
FirebaseApp.initializeApp(this);

And put:

FirebaseDatabase database = FirebaseDatabase.getInstance();

If qbix puts his comment into an answer, you should accept his rather than mine seeing has how he beat me by a few minutes.

Also:

If you are using the old firebase and need help switching, this guide is spot on and will help you with the switch. It's a fairly simple switch.

https://firebase.google.com/support/guides/firebase-android

这篇关于尽管在Application类中调用了FirebaseApp.initializeApp(),FirebaseApp仍未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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