2016-09-26 36 views
11

中被調用,但FirebaseApp未初始化我正在創建一個Android應用程序,並且我正在嘗試使用Firebase實現用戶身份驗證。據我所知,我的應用已連接到我的Firebase服務器。儘管FirebaseApp.initializeApp()在應用程序類

嘗試通過按鈕按鈕從SignIn活動切換到SignUp活動時遇到運行時錯誤。該應用程序崩潰,我遇到一個運行時錯誤。

據我所知,運行時錯誤來自於SignUp活動的onCreate()調用,當我嘗試使用FirebaseAuth.getInstance()初始化FirebaseAuth對象時。此調用失敗因

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); 
    } 
} 

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; 
    } 
} 
+0

您正在混合來自傳統2.x.x的API調用。 SDK與新的9.x.x SDK不兼容。您應該只使用新的SDK。看到這個相關的答案:http://stackoverflow.com/a/39321686/4815718 –

回答

5

正如qbix說,你多用的API調用從你將要使用的版本中選擇。如果可能的話,你應該使用更新的API,因爲它將來會得到更多的支持。

在這裏看到的文檔:

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

刪除:

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

並提出:

FirebaseDatabase database = FirebaseDatabase.getInstance(); 

如果qbix把他的評論爲答案,就應該接受他而不是我的看到有他如何擊敗我幾分鐘。

另外:

如果你使用舊的火力點和需要幫助的開關,這個指南是現貨上,並會幫助你的開關。這是一個相當簡單的開關。

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

+0

'classpath'com.google.gms:google-services:3.0.0'' for project gradle。 'compile'c​​om.google.android.gms:play-services:9.8.0' 編譯'com.google.firebase:firebase-core:9.8.0'' for app gradle,但我仍然遇到問題。任何想法? – Si8

+0

上面的評論解決這個問題 –

8

我知道已經有一個公認的答案。但是,我遇到了同樣的錯誤消息:java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process (name). Make sure to call FirebaseApp.initializeApp(Context). 我嘗試了幾種在SO上找到的解決方案,並且雙重檢查了所有內容,直到最終發現Firebase Console中定義的軟件包名稱與我的清單中定義的軟件包名稱不匹配文件。

嘗試轉到您的Firebase控制檯 - >項目設置 - >檢查包名是否匹配。

希望它可以幫助一些:)

0

它更容易使用包含在Android Studio中火力地堡嚮導:它增加了所有的依賴在gradle這個文件,創建一個火力點項目如果需要的話,將應用程序與項目連接等。

在Android Studio打開菜單工具/ Firebase並按照說明進行操作。

然後FirebaseApp.initializeApp()將返回一個有效值。

+1

除了我們中的一些人正在使用Xamarin之類的東西,所以沒有像firebase嚮導那樣的東西。 –