2016-09-26 12 views
11

Androidアプリケーションを作成していますが、現在Firebaseを使用してユーザー認証を実装しようとしています。私の知る限り、私のアプリケーションはFirebaseサーバに接続されています。FirebaseApp.initializeApp()がアプリケーションクラスで呼び出されているにもかかわらず、FirebaseAppが初期化されない

SignInアクティビティからSignUpアクティビティにボタンを押して切り替えようとすると、ランタイムエラーが発生します。アプリがクラッシュし、ランタイムエラーが発生しました。

FirebaseAuth.getInstance()FirebaseAuthオブジェクトを初期化しようとすると、ランタイムエラーはSignUpアクティビティのonCreate()コールから発生します。この呼び出しが原因

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

に失敗しかし、私は私のApplicationクラス私は大丈夫だろうと思ったonCreate()方法でこの呼び出しを行います。 SignupのonCreate()コールにはinitalizeApp()コールを追加しましたが、サイコロはありません。私はこの問題で他の人を探しましたが、類似のものは見つかりませんでした。助けてくれてありがとう。

の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(マイApplicationクラス)

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呼び出しを混合しています。新しい9.x.x SDKの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); 

とput:

FirebaseDatabase database = FirebaseDatabase.getInstance(); 

をqbixが答えに彼のコメントを置く場合は、あなたが彼を受け入れる必要があります私の見方ではなく、彼が私を数分間倒す方法を持っています。

また:あなたはが古いfirebaseを使用してあるとヘルプの切り替えが必要な場合は

、このガイドでは、その場であり、スイッチをお届けします。それはかなり簡単なスイッチです。

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

+0

'classpath 'com.google.gms:google-services:3.0.0'' for project gradle。 'compile 'com.google.android.gms:play-services:9.8.0' アプリのgradle用に 'com.google.firebase:firebase-core:9.8.0''をコンパイルしますが、まだ問題が発生します。何か案が? – Si8

+0

上記のコメント –

8

私は受け入れ答えが既にあります知っています。しかし、同じエラーメッセージが表示されました:java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process (name). Make sure to call FirebaseApp.initializeApp(Context). Firebase Consoleで定義されたパッケージ名が、私のマニフェストで定義されているパッケージ名と一致しないことがわかってしまうまで、SOで見つかったいくつかの解決策を試してみました。ファイル。

Firebaseコンソール - >プロジェクト設定 - >パッケージ名が一致するかどうかを確認してみてください。

が、それはいくつかのを助けることを願って:)

0

Androidのメーカーに含まれているFirebaseウィザードを使用する方が簡単です:、それはGradleのファイルのすべての依存関係を追加し、必要に応じてfirebaseプロジェクトを作成しますアプリケーションとプロジェクトなどを接続します。

Androidスタジオで開くメニューツール/ Firebaseと指示に従ってください。

FirebaseApp.initializeApp()は有効な値を返します。

+1

私たちの中には、Xamarinのようなものを使用している人はいないので、Firebase Wizardのようなものはありません。 –

関連する問題