2017-10-13 69 views
-3

我正在使用Android Studio和Firebase。當我運行模擬器時,代碼不會創建新用戶。該程序卡在progressDialog「註冊請稍候」。我可以在Firebase中手動添加用戶。我已啓用Firebase控制檯中的電子郵件/密碼auth,但仍存在問題程序停留在ProgressDialog

這是MainActivity.java文件。

package com.example.application.firebaseauthdemo; 

import android.app.ProgressDialog; 
import android.support.annotation.NonNull; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.text.TextUtils; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import com.google.android.gms.tasks.OnCompleteListener; 
import com.google.android.gms.tasks.Task; 
import com.google.firebase.auth.AuthResult; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.auth.FirebaseUser; 

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    //defining view objects 
    private EditText editTextEmail; 
    private EditText editTextPassword; 
    private Button buttonSignup; 
    private ProgressDialog progressDialog; 


    //defining firebaseauth object 
    private FirebaseAuth firebaseAuth; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //initializing firebase auth object 
     firebaseAuth = FirebaseAuth.getInstance(); 

     //initializing views 
     editTextEmail = (EditText) findViewById(R.id.editTextEmail); 
     editTextPassword = (EditText) findViewById(R.id.editTextPassword); 

     buttonSignup = (Button) findViewById(R.id.buttonSignup); 

     progressDialog = new ProgressDialog(this); 

     //attaching listener to button 
     buttonSignup.setOnClickListener(this); 
    } 

    private void registerUser(){ 

     //getting email and password from edit texts 
     String email = editTextEmail.getText().toString().trim(); 
     String password = editTextPassword.getText().toString().trim(); 

     //checking if email and passwords are empty 
     if(TextUtils.isEmpty(email)){ 
      Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show(); 
      return; 
     } 

     if(TextUtils.isEmpty(password)){ 
      Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show(); 
      return; 
     } 

     //if the email and password are not empty 
     //displaying a progress dialog 

     progressDialog.setMessage("Registering Please Wait..."); 
     progressDialog.show(); 

     //creating a new user 
     firebaseAuth.createUserWithEmailAndPassword(email, password) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         //checking if success 
         if(task.isSuccessful()){ 
          //display some message here 
          FirebaseUser user = firebaseAuth.getCurrentUser(); 
          Toast.makeText(MainActivity.this,"Successfully registered",Toast.LENGTH_LONG).show(); 
         }else{ 
          //display some message here 
          Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show(); 
         } 
         progressDialog.dismiss(); 
        } 
       }); 

    } 

    @Override 
    public void onClick(View view) { 
     //calling register method on click 
     registerUser(); 
    } 
} 

(2)項目(的build.gradle) file..I've已經添加類路徑。

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:2.3.3' 
     classpath 'com.google.gms:google-services:3.1.0' 
     // NOTE: Do not place your application dependencies here; they belong 
     // in the individual module build.gradle files 
    } 
} 

allprojects { 
    repositories { 
     jcenter() 
     maven { 
      url "https://maven.google.com" // Google's Maven repository 
     } 
    } 
} 

task clean(type: Delete) { 
    delete rootProject.buildDir 
} 

(3)應用程序(的build.gradle文件......在這裏,我已經添加了所有火力的依賴。

apply plugin: 'com.android.application' 

android 
     { 
    compileSdkVersion 26 
    buildToolsVersion "26.0.1" 
    defaultConfig 
      { 
     applicationId "com.example.application.firebaseauthdemo" 
     minSdkVersion 15 
     targetSdkVersion 26 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 

    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:26.1.0' 
    compile 'com.android.support:animated-vector-drawable:26.1.0' 
    compile 'com.android.support:mediarouter-v7:26.1.0' 
    compile 'com.google.android.gms:play-services:11.0.4' 

    compile 'com.android.support.constraint:constraint-layout:1.0.2' 
    compile 'com.google.firebase:firebase-auth:11.0.4' 
    compile 'com.google.firebase:firebase-core:11.0.4' 
    compile 'com.google.firebase:firebase-crash:11.0.4' 
    compile 'com.google.firebase:firebase-messaging:11.0.4' 
    compile 'com.google.android.gms:play-services-maps:11.0.4' 
    compile 'com.google.android.gms:play-services-location:11.0.4' 
    testCompile 'junit:junit:4.12' 

} 
apply plugin: 'com.google.gms.google-services' 

我已經添加了所有必需的依賴關係。另外,添加與Firebase中相同的類路徑。另外,我清理了這個項目並且rebuild它。但問題仍然無法解決。另外,我已經在Firebase控制檯中啓用了電子郵件/密碼身份驗證。仍然該應用程序繼續卡在progressDialog,用戶無法在Firebase auth中註冊。

+0

@UltimateDevil:請不要將內聯代碼格式添加到非代碼的東西。 Firebase只是一個品牌和專有名詞,所以它會得到一個大寫字母,就是這樣。謝謝。 – halfer

+0

Shagun,請閱讀[在什麼情況下我可以在我的問題中添加「緊急」或其他類似的短語,以獲得更快的答案?](// meta.stackoverflow.com/q/326569) - 總結是這不是解決志願者問題的理想方式,而且可能對獲得答案起反作用。請不要將這添加到您的問題。 – halfer

+1

好吧,@halfer這個錯誤不會再在未來 – UltimateDevil

回答

0

您應該爲加載的工作創建AsyncTask。

例如:

AsyncTask<String, String, String> asyncTask = new AsyncTask() { 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       progressDialog.setMessage("Registering Please Wait..."); 
       progressDialog.show(); 
      } 

      @Override 
      protected Object doInBackground(Object[] params) { 
       firebaseAuth.createUserWithEmailAndPassword(email, password) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         //checking if success 
         if(task.isSuccessful()){ 
          //display some message here 
          FirebaseUser user = firebaseAuth.getCurrentUser(); 
          Toast.makeText(MainActivity.this,"Successfully registered",Toast.LENGTH_LONG).show(); 
         }else{ 
          //display some message here 
          Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show(); 
         } 

        } 
       }); 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Object o) { 
       super.onPostExecute(o); 
       progressDialog.dismiss(); 
      } 
     }; 
     asyncTask.execute(); 
+0

仍然是同樣的問題... – Shagun

+0

它是否給運行時異常? –

+0

沒有..它沒有給出任何錯誤或異常。只是用戶沒有被添加到Firebase身份驗證。progressDialog只是顯示「正在註冊,請稍候」..之後沒有任何操作發生。 – Shagun

0

您應該添加一個addOnFailureListener檢查什麼是失敗的,因爲addOnCompleteListener永遠不會被調用。

//creating a new user 
    firebaseAuth.createUserWithEmailAndPassword(email, password) 
      .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        // YOUR CODE 
       } 
      }) 
      .addOnFailureListener(this, new OnFailureListener() { 
       @Override 
       public void onFailure(@NonNull Exception e) { 
        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); 
      } 
     });