2017-12-27 31 views
0

Also die Sache ist, ich habe alle Schritte in meinem Firebase-Setup und Zeug, aber das Steuerelement bewegt sich nicht in meinem createUserWithEmailAndPassword()-Methode. Der Bildschirm friert einfach ein und nichts anderes passiert als die progressBar in Kreisen.Firebase-Zugriff funktioniert nicht

Hier meine gradle build.app Datei ist:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 26 
    buildToolsVersion '26.0.2' 
    defaultConfig { 
     applicationId "com.example.parma.gareebcalculator" 
     minSdkVersion 23 
     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 { 

    implementation 'com.google.firebase:firebase-auth:11.0.4' 
    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:support-v4:26.1.0' 

    testCompile 'junit:junit:4.12' 
} 

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

Und mein Code ist hier: -

public class RegsiterPage extends AppCompatActivity implements View.OnClickListener{ 

    private EditText userName; 
    private EditText email; 
    private EditText password; 
    private EditText repassword; 
    private Button signUp; 
    private ProgressBar progressBar; 
    private FirebaseAuth mAuth; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.activity_regsiter_page); 

     userName = (EditText) findViewById(R.id.usernameid); 
     email = (EditText) findViewById(R.id.emailid); 
     password = (EditText) findViewById(R.id.passwordid); 
     repassword = (EditText) findViewById(R.id.repasswordid); 
     signUp = (Button) findViewById(R.id.signupid); 
     progressBar = (ProgressBar) findViewById(R.id.progressBarid); 
     // Used to intialize the firebase object and register the user on the server:- 
     mAuth = FirebaseAuth.getInstance(); 

     signUp.setOnClickListener(this); 
    } 

    //Email constraints:- 
    //To check whether the entered email is of correct format: 
    private boolean isValidEmail(String email) 
    { 
     if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()) 
     { 
      return false; 
     } 
     return true; 
    } 

    //Username constraints:- 
    //To check whether the given username only contains letters or digits: 
    private boolean userNameCheck(String name){ 
     if(Character.isDigit(name.charAt(0))){ 
      return false; 
     } 
     for(int i=0;i<name.length();i++){ 
      if(!Character.isLetterOrDigit(name.charAt(i))){ 
       return false; 
      } 
     } 
     return true; 
    } 

    private boolean checkUserNameLength(String name){ 
     if(name.length()<8){ 
      return false; 
     } 
     return true; 
    } 

    //Password constraints:- 
    //To see whethter the password contains any special characters or not: 
    private boolean checkSpecial(String pass){ 
     byte[] bytes = pass.getBytes(); 
     for(byte temp:bytes){ 
      if(temp>=33 && temp<=47){ 
       return false; 
      } 
     } 
     return true; 
    } 

    //To see whethter the password is containing both alphabets and numbers: 
    private boolean checkPassword(String pass){ 
     if(pass.length()<8){ 
      return false; 
     } 
     for(int i=0;i<pass.length();i++){ 
      if(!Character.isLetterOrDigit((pass.charAt(i)))){ 
       return false; 
      } 
     } 
     return true; 
    } 

    private void onSignUp(){ 
     final String usernameString = userName.getText().toString().trim(); 
     final String emailString = email.getText().toString().trim(); 
     final String passwordString = password.getText().toString().trim(); 
     final String repasswordString = repassword.getText().toString().trim(); 

     /**If any of the either fields login or password are empty then the following couple actions might take place:- 
     * Also if the passwords mismatch then the user will be denied a successfull registeration*/ 
     if(TextUtils.isEmpty(usernameString)){ 
      userName.setError("Please enter Username"); 
      userName.requestFocus(); 
      return; 
     } 
     if(TextUtils.isEmpty(emailString)){ 
      email.setError("Please enter Email Id"); 
      email.requestFocus(); 
      return; 
     } 
     if(TextUtils.isEmpty(passwordString)){ 
      password.setError("Please enter Password"); 
      password.requestFocus(); 
      return; 
     }if(TextUtils.isEmpty(repasswordString)){ 
      repassword.setError("Please Re-enter Password"); 
      repassword.requestFocus(); 
      return; 
     } 
     if(!userNameCheck(usernameString)){ 
      userName.setError("Username can contain only alphabets or digits.Username cannot start with a number"); 
      userName.setText(""); 
      userName.requestFocus(); 
      return; 
     } 
     if(!checkUserNameLength(usernameString)){ 
      userName.setError("The username should atleast contain 8 charchacters"); 
      userName.setText(""); 
      userName.requestFocus(); 
      return; 
     } 
     if (userName.getText().toString().contains(" ")) { 
      userName.setError("Spaces NOT Allowed"); 
      userName.requestFocus(); 
      return; 
     } 
     if(!isValidEmail(emailString)){ 
      email.setError("Incorrect Email format"); 
      email.requestFocus(); 
      return; 
     } 
     if (!repasswordString.equals(passwordString)){ 
      //Toast.makeText(this,"Password Mismatch.Try Again",Toast.LENGTH_SHORT).show(); 
      password.setGravity(10); 
      password.setError("Passwords are incorrect"); 
      password.setText(""); 
      repassword.setText(""); 
      password.requestFocus(); 
      return; 
     } 
     if(!checkPassword(passwordString)){ 
      password.setError("The password should be alphanumeric and should be 8 characters long.No special symbols allowed"); 
      password.setText(""); 
      repassword.setText(""); 
      password.requestFocus(); 
      return; 
     } 
     if(!checkSpecial(passwordString)){ 
      password.setError("The password should not contain any special characters"); 
      password.setText(""); 
      repassword.setText(""); 
      password.requestFocus(); 
      return; 
     } 

     //If all, username,email,password and repassword have been entered then the following part takes place 
     progressBar.setVisibility(View.VISIBLE); 

     /*Here the user is created using a password and email and addonCompleteListener is optional part which states what to be 
     done when the user has successfully logged in*/ 
     mAuth.createUserWithEmailAndPassword(emailString,passwordString).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
      @Override 
      public void onComplete(@NonNull Task<AuthResult> task) { 
       progressBar.setVisibility(View.GONE); 
       if(task.isSuccessful()){ 
        Toast.makeText(RegsiterPage.this,"Registered Successfully.",Toast.LENGTH_SHORT).show(); 
        //To record the username in a the android application: 
        /*FirebaseUser user = mAuth.getCurrentUser(); 
        if(user!=null){ 
         UserProfileChangeRequest profile = new UserProfileChangeRequest.Builder() 
           .setDisplayName(usernameString) 
           .build(); 

         user.updateProfile(profile).addOnCompleteListener(new OnCompleteListener<Void>() { 
          @Override 
          public void onComplete(@NonNull Task<Void> task) { 
           if(task.isSuccessful()){ 
            Toast.makeText(RegsiterPage.this,"Awesome Profile Created",Toast.LENGTH_SHORT).show(); 
           }else{ 
            Toast.makeText(RegsiterPage.this,task.getException().getMessage(),Toast.LENGTH_SHORT).show(); 
           } 
          } 
         }); 
        }*/ 
        Intent intent = new Intent(getApplicationContext(),LoginPage.class); 
        startActivity(intent); 
       } 
       else if(task.getException() instanceof FirebaseAuthUserCollisionException) { 
        Toast.makeText(RegsiterPage.this,"You are already Registered",Toast.LENGTH_SHORT).show(); 
        userName.setText(""); 
        email.setText(""); 
        password.setText(""); 
        repassword.setText(""); 
       }else{ 
        Toast.makeText(RegsiterPage.this,task.getException().getMessage(),Toast.LENGTH_SHORT).show(); 
        userName.setText(""); 
        email.setText(""); 
        password.setText(""); 
        repassword.setText(""); 
       } 
      } 
     }); 

    } 

    @Override 
    public void onClick(View v) { 
     if(v == signUp){ 
      onSignUp(); 
     } 
    } 
} 

Edit: -

Hier ist meine logcat: -

E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.parma.gareebcalculator, PID: 3149 
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task 
com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzdd)' on a null object reference 
at com.google.android.gms.internal.zzdvv.zzb(Unknown Source) 
at com.google.android.gms.internal.zzdwc.zza(Unknown Source) 
at com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(Unknown 
    Source) 
at com.example.parma.gareebcalculator.RegsiterPage.onsignup(RegsiterPage.java:192) 
at com.example.parma.gareebcalculator.RegsiterPage.onClick(RegsiterPage.java:241) 
at android.view.View.performClick(View.java:5610) 
at android.view.View$PerformClick.run(View.java:22265) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Lösung gefunden

Lösung: wenn jemand entlang this..just dies tun, kommt ... meine gesamte Code und alles perfekt war Pech ... aber was falsch war, war mit meinem Emulator ... es lief auf API 24 ... und das war eigentlich nicht nachvollziehbar aufgrund der Tatsache, dass die App nur kassierte, aber nie gesagt hat, wie .... also versuche einfach die API auf 26 zu ändern (funktionierte für mich) und nutze oder erstelle eine neue

Emulator
+0

Haben Sie die E-Mail-Anmelde-Methode in Ihrer Firebase-Konsole aktiviert? Fügen Sie auch einen onFailure-Rückruf zusammen mit dem onComplete-Rückruf hinzu, der Ihnen vielleicht eine Idee geben könnte, was falsch ist. – Kushan

+0

@Kushan ja ich tat ... es ist immer noch unklar, was falsch ist –

+0

@Kushan .. danke für die Überprüfung meines Codes Sir. .Ich habe die Lösung es ist in der bearbeiteten Beschreibung .. Vielen Dank –

Antwort

0

Ihr mAuth ist Null und das ist der Grund für diesen Absturz

ein chec Halten k

if(mAuth !=null){ 

    } 
+0

Er initialisiert es in onCreate – Kushan

+0

Guys ... Sie werden nicht glauben, was schief gelaufen ist .. mein ganzer Code war perfekt ... aber mein Emulator, der war Die Arbeit an API 24 erlaubte mir nicht, mich mit dem Firebase zu verbinden, obwohl alles richtig schien. –

+0

Ja, was war es? – ABDevelopers