gpt4 book ai didi

android - 尽管在应用程序类中调用了 FirebaseApp.initializeApp(),但 FirebaseApp 未初始化

转载 作者:可可西里 更新时间:2023-11-01 19:05:55 25 4
gpt4 key购买 nike

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

我在尝试通过按下按钮从 SignIn Activity 切换到 SignUp Activity 时遇到运行时错误。应用程序崩溃,我遇到运行时错误。

据我所知,当我尝试使用 FirebaseAuth 初始化 FirebaseAuth 对象时,运行时错误来自 SignUp Activity 的 onCreate() 调用。获取实例()。此调用因

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

但是,我在我的应用程序类的 onCreate() 方法中进行了此调用,我认为这没问题。我将 initalizeApp() 调用添加到 SignUp 的 onCreate() 调用中,但没有骰子。我找过其他有这个问题的人,但没有发现任何类似的东西。感谢您的帮助。

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(我的应用程序类)

public class PhoneAFriend extends Application {

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

注册.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;
}
}

最佳答案

我知道已经有一个公认的答案。但是,我遇到了同样的错误消息:java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process (name)。确保调用 FirebaseApp.initializeApp(Context)。我尝试了在 SO 上找到的几种解决方案,并仔细检查了所有内容,直到我最终发现 Firebase 控制台中定义的包名称与我的 list 文件中定义的包名称不匹配。

尝试转到您的 Firebase 控制台 -> 项目设置 -> 检查包名称是否匹配。

希望对大家有所帮助:)

关于android - 尽管在应用程序类中调用了 FirebaseApp.initializeApp(),但 FirebaseApp 未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39708272/

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