-1

Hi so i am doing a login / signup system using firebase, for some reason startActivity just doesnt want to work. here is the Main Activity (LoginScreen)

package com.example.noam.photoupload;

import android.app.ProgressDialog;
import android.content.Intent;
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.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class LoginScreen extends AppCompatActivity implements 
View.OnClickListener {

private ProgressDialog Loading;
private FirebaseAuth mFireAuth;
private Button Registar;
private EditText Email;
private EditText Passowrd;
private Button Signin;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_screen);

    Loading= new ProgressDialog(this);
    Registar=(Button) findViewById(R.id.Register);
    Email=(EditText)findViewById(R.id.EditEmail);
    Passowrd=(EditText)findViewById(R.id.EditPassword);
    Signin=(Button)findViewById(R.id.SignInlink);
    Registar.setOnClickListener(this);
    Signin.setOnClickListener(this);
    mFireAuth= FirebaseAuth.getInstance();
    if(mFireAuth.getCurrentUser() !=null){
        finish();
        startActivity(new Intent(getApplicationContext(),ProfileActive.class));
    }



}
private void RegistarUser(){
    String email=Email.getText().toString().trim();
    String password=Passowrd.getText().toString().trim();

    if(TextUtils.isEmpty(email)){
        //email is empty
        Toast.makeText(this,"Please enter email",Toast.LENGTH_SHORT).show();
        return;

    }
    if(TextUtils.isEmpty(password)){
        //Passowrd is empty
        Toast.makeText(this,"Please enter Password",Toast.LENGTH_SHORT).show();
        return;
    }
    Loading.setMessage("Reguistering user...");
    Loading.show();
    mFireAuth.createUserWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Loading.dismiss();
                    if(task.isSuccessful()){
                        //user is Registerd ,and login
                        //add my start to the next screen

                            finish();
                            startActivity(new Intent(getApplicationContext(),ProfileActive.class));
                        }



                    else{

                        Toast.makeText(LoginScreen.this,"failed",Toast.LENGTH_SHORT).show();
                    }

                }
            });




}

@Override
public void onClick(View view) {
    if(view ==Registar){
        RegistarUser();
    }
    if(view == Signin){
        //LoginInActivity
        finish();
        Intent i = new Intent(LoginScreen.this, SignIn.class);
        startActivity(i);

    }

}

Main Class XML

<?xml version="1.0" encoding="utf-8"?>




<LinearLayout
         <Button
           android:layout_margin="15dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:id="@+id/Register"
           android:text="Register User"/>

       <Button
           android:layout_margin="15dp"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="Already Registered? Sign in here"
           android:id="@+id/SignInlink"
           android:gravity="center"/>

   </LinearLayout>

</RelativeLayout>

And this is the second Class (important to say , For some reason it also gives me an error :

"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'

on a null object reference" )

package com.example.noam.photoupload;

import android.app.ProgressDialog;
import android.content.Intent;
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.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class SignIn extends AppCompatActivity implements 
View.OnClickListener {
private ProgressDialog Loading;
private FirebaseAuth mFireAuth;
private Button Login;
private EditText Email;
private EditText Passoword;
private TextView SignUp;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);Loading= new ProgressDialog(this);

    setContentView(R.layout.activity_login_screen);

    Login=(Button) findViewById(R.id.login);
    Email=(EditText)findViewById(R.id.EditEmail);
    Passoword=(EditText)findViewById(R.id.EditPassword);
    SignUp=(TextView)findViewById(R.id.Signuplink);
    mFireAuth= FirebaseAuth.getInstance();
    Login.setOnClickListener(this);
    SignUp.setOnClickListener(this);




    ;
}
private void UserLogin(){
    String email=Email.getText().toString().trim();
    String password=Passoword.getText().toString().trim();

    if(TextUtils.isEmpty(email)){
        //email is empty
        Toast.makeText(this,"Please enter email",Toast.LENGTH_SHORT).show();
        return;

    }
    if(TextUtils.isEmpty(password)){
        //Passowrd is empty
        Toast.makeText(this,"Please enter Password",Toast.LENGTH_SHORT).show();
        return;
    }
    Loading.setMessage("Reguistering user...");
    Loading.show();
    mFireAuth.signInWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Loading.dismiss();
                    if(task.isSuccessful()){
                        //user is Registerd ,and login
                        //add my start to the next screen

                            finish();
                            startActivity(new Intent(getApplicationContext(),ProfileActive.class));
                        }



                    else{

                        Toast.makeText(SignIn.this,"failed",Toast.LENGTH_SHORT).show();
                    }

                }
            });




}

@Override
public void onClick(View view) {
    if(view ==Login){
        UserLogin();
    }
    if(view == SignUp){
        //LoginInActivity
        finish();
        startActivity(new Intent(this,LoginScreen.class));
    }

}

Second Class XML

<Button
            android:layout_margin="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/login"
            android:text="Login"/>

        <TextView
            android:layout_margin="15dp"
            android:onClick="onClick"
            android:clickable="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Don't have an account yet ? Signup here "
            android:id="@+id/Signuplink"
            android:gravity="center"/>

(after trying the suggested solution): Update 1: I "deleted" SignIn code in order to check if the problem is inside SignIn, and still the mainclass (LoginScreen) didn't start the next activity, and also for some reason it gave me the "Enter Email" Toast,meaning it entered the other if for some reason (?) But the app didn't crash this time!

Update 2: I manged to find the bug that call the RegisterUser() , and fixed it , but again,same bug whenever i click "SignInLink" in the Loginscreen,it crashes the app if there is the same code in SignIn class, So again i decided to "delete" all code from SignIn class(leaving the class with only the xml view) , and now when ever i press the SignInLink, it just call the mainclass again without functioning (maybe it calls the login screen but doens't change the view of the buttons? i really don't know!)

jeff style
  • 61
  • 1
  • 7
  • forgot to say what isnt working , When i press the "SignInlink" button , it just doesnt move me to the second class for some reason. hope its more clear now – jeff style Sep 30 '17 at 01:32

1 Answers1

2

Change all code like this

finish();
startActivity(new Intent(getApplicationContext(),ProfileActive.class));

to

startActivity(new Intent(getApplicationContext(),ProfileActive.class));
finish();

And change to this in LoginScreen class

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.Register:
            RegistarUser();
            break;
        case R.id.SignInlink:
            Intent i = new Intent(LoginScreen.this, SignIn.class);
            startActivity(i);
            finish();
            break;
    }
}

And change in SignInclass

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.login:
            UserLogin();
            break;
        case R.id.Signuplink:
            startActivity(new Intent(this,LoginScreen.class));
            finish();
            break;
    }
}

Edit

 <TextView
        android:layout_margin="15dp"
        android:clickable="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Don't have an account yet ? Signup here "
        android:id="@+id/Signuplink"
        android:gravity="center"/>

Just remove android:onClick="onClick" in your code .

I try the code like this is ok . You can try it .

Edit2

LoginScreen

public class LoginScreen extends AppCompatActivity implements
    View.OnClickListener {

private ProgressDialog Loading;
//    private FirebaseAuth mFireAuth;
private Button Registar;
private EditText Email;
private EditText Passowrd;
private Button Signin;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_screen);

    Loading = new ProgressDialog(this);
    Registar = (Button) findViewById(R.id.Register);
    //        Email=(EditText)findViewById(R.id.EditEmail);
    //        Passowrd=(EditText)findViewById(R.id.EditPassword);
    Signin = (Button) findViewById(R.id.SignInlink);
    Registar.setOnClickListener(this);
    Signin.setOnClickListener(this);


}

private void RegistarUser() {
//        String email = Email.getText().toString().trim();
//        String password = Passowrd.getText().toString().trim();

    if (TextUtils.isEmpty("email")) {
        //email is empty
        Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
        return;

    }
    if (TextUtils.isEmpty("password")) {
        //Passowrd is empty
        Toast.makeText(this, "Please enter Password", Toast.LENGTH_SHORT).show();
        return;
    }
    Loading.setMessage("Reguistering user...");
    Loading.show();


}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.Register:
            RegistarUser();
            break;
        case R.id.SignInlink:
            Intent i = new Intent(LoginScreen.this, SignIn.class);
            startActivity(i);
            finish();
            break;
    }
}
}

SignIn

public class SignIn extends AppCompatActivity implements
    View.OnClickListener {
private ProgressDialog Loading;
private Button Login;
private EditText Email;
private EditText Passoword;
private TextView SignUp;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Loading = new ProgressDialog(this);

    setContentView(R.layout.activity_sign_in);

    Login = (Button) findViewById(R.id.login);
    //        Email=(EditText)findViewById(R.id.EditEmail);
    //        Passoword=(EditText)findViewById(R.id.EditPassword);
    SignUp = (TextView) findViewById(R.id.Signuplink);
    Login.setOnClickListener(this);
    SignUp.setOnClickListener(this);


    ;
}

private void UserLogin() {
//        String email = Email.getText().toString().trim();
//        String password = Passoword.getText().toString().trim();

    if (TextUtils.isEmpty("email")) {
        //email is empty
        Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
        return;

    }
    if (TextUtils.isEmpty("password")) {
        //Passowrd is empty
        Toast.makeText(this, "Please enter Password", Toast.LENGTH_SHORT).show();
        return;
    }
    Loading.setMessage("Reguistering user...");
    Loading.show();


}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.login:
            UserLogin();
            break;
        case R.id.Signuplink:
            startActivity(new Intent(this, LoginScreen.class));
            finish();
            break;
    }
}
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
  • First of all thank you on answering ,But it seems like it still doesn't work , when i click "signuplink" , the app stops working . the error i am getting : FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.noam.photoupload/com.example.noam.photoupload.SignIn}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference – jeff style Sep 30 '17 at 09:29
  • Hi ,i removed it and now i am not getting the error in the android monitor , but still whenever i click it "signuplink" , the app just crash – jeff style Sep 30 '17 at 11:24
  • Show your detail Log info – KeLiuyue Sep 30 '17 at 11:27
  • 09-30 14:31:16.001 1154-1338/com.example.noam.photoupload I/FA: Tag Manager is not found and thus will not be used 09-30 14:31:16.001 1154-1338/com.example.noam.photoupload D/FA: Logging event (FE): _e, Bundle[{_o=auto, _et=13938, _sc=LoginScreen, _si=-4347483831692551899}] 09-30 14:31:19.471 2378-2378/? I/[SC]ConnectionJobService: scheduleJob : false 09-30 14:31:19.481 2378-2378/? I/[SC]ConnectionJobService: Register connection receiver 09-30 14:31:19.621 2378-2411/? I/[SC]ConnectionJobService: CONNECTION_CHANGE_RECEIVER – jeff style Sep 30 '17 at 11:32
  • You should give me that `RuntimeException` is the Log. You can copy it to here . – KeLiuyue Sep 30 '17 at 11:40
  • how do i give the runtimeException log? sorry for miss understanding – jeff style Sep 30 '17 at 12:24
  • Yes,give the `RuntimeException` log – KeLiuyue Sep 30 '17 at 12:28
  • where can i find it tho? i cant find it in the android monitor – jeff style Sep 30 '17 at 13:03
  • You can check it .And I will help you tomorrow .It 's 21:10 in my city .Sorry . – KeLiuyue Sep 30 '17 at 13:12
  • its fine , thank you for trying tho , i still cant find the runtimeexception log for some reason :( – jeff style Sep 30 '17 at 13:37