0

我的應用不會將用戶添加到Firebase數據庫。我的數據庫顯示三個用戶的應用程序崩潰,但控制檯不顯示任何用戶。任何幫助表示讚賞。Firebase - 創建帳戶和登錄問題

public class LoginPage extends AppCompatActivity implements View.OnClickListener 
    { 

    private FirebaseAuth mAuth; 

    private DatabaseReference mDatabase; 

    private static final String TAG = "GoogleActivity"; 

    private FirebaseAuth.AuthStateListener mAuthListener; 

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login_page); 
     mDatabase = FirebaseDatabase.getInstance().getReferenceFromUrl("https://student-help-portal.firebaseio.com"); 
     idusername = (EditText) findViewById(R.id.idusername); 
     idpassword = (EditText) findViewById(R.id.idpassword); 
     mAuth = FirebaseAuth.getInstance(); 
     buttonlogin = (Button) findViewById(R.id.buttonlogin); 
     buttonsignup = (Button) findViewById(R.id.buttonsignup); 
     mAuthListener = new FirebaseAuth.AuthStateListener() { 
      @Override 
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
       FirebaseUser user = firebaseAuth.getCurrentUser(); 
       if (user != null) { 
        // User is signed in 
        Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); 
       } else { 
        // User is signed out 
        Log.d(TAG, "onAuthStateChanged:signed_out"); 
       } 
      } 
     }; 
    } 
    public class User { 

     public String username; 
     public String password; 

     public User() { 
      // Default constructor required for calls to DataSnapshot.getValue(User.class) 
     } 

     public User(String username, String password) { 
      this.username = username; 
      this.password = password; 
     } 

    } 
    private String username, password; 
    Button buttonlogin, buttonsignup; 
    EditText idusername, idpassword; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_login_page, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
    private boolean validateForm() { 
     boolean valid = true; 
     if (TextUtils.isEmpty(username)) { 
      idusername.setError("Required."); 
      valid = false; 
     } else { 
      idusername.setError(null); 
     } 
     if (TextUtils.isEmpty(password)) { 
      idpassword.setError("Required."); 
      valid = false; 
     } else { 
      idpassword.setError(null); 
     } 

     return valid; 
    } 
    private void createAccount(String email, String password) { 
     if (!validateForm()) { 
      return; 
     } 
     Log.d(TAG, "createAccount:" + email); 
     mAuth.createUserWithEmailAndPassword(email, password) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful()); 
         if (!task.isSuccessful()) { 
          Toast.makeText(LoginPage.this, "Failed to create account", 
            Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 
    } 
    @Override 
    protected void onStart() { 
     super.onStart(); 
     mAuth.addAuthStateListener(mAuthListener); 
     buttonsignup.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       username = idusername.getText().toString(); 
       password = idpassword.getText().toString(); 
       createAccount(username,password); 
      } 
     }); 
     buttonlogin.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       username = idusername.getText().toString(); 
       password = idpassword.getText().toString(); 
       signIn(username,password); 
       Intent tohomepage = new Intent(LoginPage.this, HomePage.class); 
       startActivity(tohomepage); 
      } 
     } 
     ); 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "LoginPage Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content,. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app deep link URI is correct. 
       Uri.parse("android-app://curlybraces.studenthelpportal/http/host/path") 
     ); 
     Action viewAction2 = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "LoginPage Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://curlybraces.studenthelpportal/http/host/path") 
     ); 
    } 
    private void signIn(String username, String password) { 
     Log.d(TAG, "signIn:" + username); 
     if (!validateForm()) { 
      return; 
     }mAuth.signInWithEmailAndPassword(username, password) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful()); 
         if (!task.isSuccessful()) { 
          Log.w(TAG, "signInWithEmail:failed", task.getException()); 
          Toast.makeText(LoginPage.this, "failed", 
            Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 
    } 
    private void signOut() { 
     mAuth.signOut(); 
    } 
    @Override 
    public void onStop() { 
     super.onStop(); 
     if (mAuthListener != null) { 
      mAuth.removeAuthStateListener(mAuthListener); 
     } 
     /*Action viewAction2 = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "LoginPage Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://curlybraces.studenthelpportal/http/host/path") 
     ); 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "LoginPage Page", // TODO: Define a title for the content shown. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app deep link URI is correct. 
       Uri.parse("android-app://curlybraces.studenthelpportal/http/host/path") 
     );*/ 
    } 
    public String getUser(FirebaseUser user) { 
     if (user != null) { 
      return user.getEmail(); 
     } 
     return null; 
    } 
    @Override 
    public void onClick(View v) { 
     int i = v.getId(); 
     if (i == R.id.buttonsignup) { 
      createAccount(username,password); 
     } else if (i == R.id.buttonlogin) { 
      signIn(username,password); 
     } /*else if (i == R.id.sign_out_button) { 
      signOut(); 
     }*/ 
    } 

    } 
+0

您是否有關於墜機的更多信息? – JamieB

+0

我遇到的主要問題是,當我註冊一個新用戶時,該條目被添加到數據庫,但是!task.isSuccessful並沒有給出正面結果。在這段代碼中,我將意圖活動添加到了else部分的主頁中,但它不起作用。我不知道爲什麼 –

回答

0

我有同樣的問題,這是通過改變仿真器API版本解決。我相信API 23和Firebase之間存在問題。將其更改爲21 API相同的代碼完美工作。

+0

歡迎來堆棧交換。我想知道,這個問題是在什麼地方記錄的?這是一個已知的問題嗎?如果是這樣,請包含一個鏈接。 – Jon

+0

我遇到的主要問題是,當我註冊一個新用戶時,該條目被添加到數據庫,但是!task.isSuccessful並沒有給出正面結果。在這段代碼中,我將意圖活動添加到了else部分的主頁中,但它不起作用。我想知道爲什麼 –

相關問題