title: Why does my app crash when i use start button in main activity to access login activity?
body: <p>When i click button START on main activity, crashes before it opens the login activity. What do i do to stop the app from crashing?</p>
  
  <p>I have connected the application to firebase and written the code i think is right. Kindly help solve the code wherever i might have mistaken.</p>
  
  <p>I expect when i click button in the main activity to open login activity that requires user email and password and pop up of log in successful. Instead i get a " has stopped" message.</p>
  
  <p>code for the <code>MainActivity.java</code></p>
  
  <pre><code>package com.kiki.doctorlocation;
  
  import androidx.annotation.NonNull;
  import androidx.appcompat.app.AppCompatActivity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  import com.google.firebase.auth.FirebaseAuth;
  
  public class MainActivity extends AppCompatActivity {
  
      FirebaseAuth mAuth;
      FirebaseAuth.AuthStateListener mAuthListner;
  
      protected void onStart() {
          super.onStart();
          mAuth.addAuthStateListener(mAuthListner);
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Button btn = (Button) findViewById(R.id.startbtn);
          mAuth = FirebaseAuth.getInstance();
  
          mAuthListner = new FirebaseAuth.AuthStateListener() {
              @Override
              public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                  if (firebaseAuth.getCurrentUser() == null) {
                      startActivity(new Intent(MainActivity.this, login.class));
                  }
              }
          };
  
  
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  mAuth.signOut();
              }
          });
  
      }
  }
  </code></pre>
  
  <p>code for <code>MainActivity.xml</code></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/docto"
      tools:context=".MainActivity"&gt;
  
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
          &lt;Button
              android:id="@+id/startbtn"
              android:layout_width="200dp"
              android:layout_height="wrap_content"
              android:text="START"
              android:layout_gravity="center_horizontal"
              android:layout_centerInParent="true"
              android:background="@drawable/roundbtn"/&gt;
      &lt;/RelativeLayout&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  
  <p>code for the <code>login.java</code>    </p>
  
  <pre><code>package com.kiki.doctorlocation;
  
  import androidx.annotation.NonNull;
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.app.ProgressDialog;
  import android.os.Bundle;
  import android.text.TextUtils;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  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;
  
  public class login extends AppCompatActivity {
  
      private EditText txtemail, txtpassword;
      private TextView txtsign_up;
      private Button btnlogin;
  
      private ProgressDialog progressDialog;
  
      private FirebaseAuth firebaseAuth;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_login);
  
          firebaseAuth = FirebaseAuth.getInstance();
  
          progressDialog = new ProgressDialog(this);
  
          txtemail = (EditText) findViewById(R.id.txtemail);
          txtpassword = (EditText) findViewById(R.id.txtpassword);
          txtsign_up = (TextView) findViewById(R.id.txtsign_up);
          btnlogin = (Button) findViewById(R.id.btnlogin);
  
          btnlogin.setOnClickListener((View.OnClickListener) this);
          txtsign_up.setOnClickListener((View.OnClickListener) this);
      }
  
      public void registerUser() {
          String email = txtemail.getText().toString().trim();
          String password = txtpassword.getText().toString().trim();
  
          if(TextUtils.isEmpty(email)){
              //if email is empty
              Toast.makeText(this, "Email is required", Toast.LENGTH_SHORT).show();
              return;
          }
          if(TextUtils.isEmpty(password)){
              //if password is empty
              Toast.makeText(this,"Password is required", Toast.LENGTH_SHORT).show();
              return;
          }
  
          //if user is validated
          progressDialog.setMessage("Logging user in...");
          progressDialog.show();
  
          firebaseAuth.createUserWithEmailAndPassword(email, password)
                  .addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                          if(task.isSuccessful())
                          {
                              Toast.makeText(login.this, "Log in Successful", Toast.LENGTH_SHORT).show();
                          }else{
                              Toast.makeText(login.this, "Log in Failed, Please try again", Toast.LENGTH_SHORT).show();
                          }
  
  
                      }
                  });
      }
  
      public void onClick(View view){
          if(view == btnlogin ){
              registerUser();
          }
          if(view == txtsign_up){
              //open signup activity
          }
      }
  }
  </code></pre>
  
  <p>code for <code>login.xml</code></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/doc"
      android:orientation="vertical"
      android:gravity="center"
      tools:context=".login"&gt;
  
      &lt;EditText
          android:id="@+id/txtemail"
          android:layout_width="match_parent"
          android:layout_height="50dp"
          android:ems="10"
          android:background="@drawable/roundsedttxt"
          android:hint="Enter email"
          android:textAlignment="center"
          android:inputType="textEmailAddress"
          android:layout_margin="10dp"/&gt;
  
  
      &lt;EditText
          android:id="@+id/txtpassword"
          android:layout_width="match_parent"
          android:layout_height="50dp"
          android:ems="10"
          android:background="@drawable/roundsedttxt"
          android:hint="Password"
          android:textAlignment="center"
          android:inputType="textPassword"
          android:layout_margin="10dp"/&gt;
  
      &lt;Button
          android:id="@+id/btnlogin"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/roundbtn"
          android:text="Login"
          android:layout_margin="10dp"/&gt;
  
      &lt;TextView
          android:id="@+id/txtsign_up"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Create a new account"
          android:textStyle="bold"
          android:textAllCaps="true"
          android:textAlignment="center"
          android:textAppearance="@color/common_google_signin_btn_text_dark"
          android:textColor="#FFFFFF"
          android:layout_margin="10dp"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>