How to open an Activity from a java class?

0

I have a java class called Firebase, this class only does firebase functions ...

I am authenticating a new user in firebase, so far so good .. O problem is that I can not move to another Activity, it's giving the following error:

05-24 23:56:38.335 15742-15742/com.hotelaria.neoris.checkincheckout E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hotelaria.neoris.checkincheckout, PID: 15742
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
        at android.app.Activity.startActivityForResult(Activity.java:4488)
        at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
        at android.app.Activity.startActivityForResult(Activity.java:4445)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
        at android.app.Activity.startActivity(Activity.java:4806)
        at android.app.Activity.startActivity(Activity.java:4774)
        at com.hotelaria.neoris.checkincheckout.models.objects.Firebase$1.onComplete(Firebase.java:35)
        at com.google.android.gms.tasks.zzf.run(Unknown Source:23)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Firebase.class:

package com.hotelaria.neoris.checkincheckout.models.objects;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
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.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.hotelaria.neoris.checkincheckout.activitys.SigninActivity;;

public class Firebase extends AppCompatActivity {

    private final FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance();
    private final FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
    private final DatabaseReference mDatabaseReference = mFirebaseDatabase.getReference();

    public void writeNewUser(final Activity activity, final Context activityContext, String email, String password){
        this.mFirebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()) {
                            Toast toast = Toast.makeText(activityContext,"SUCESSO, CONTA CADASTRADA!", Toast.LENGTH_SHORT);
                            toast.show();

                            Intent intent = new Intent(activity.getApplicationContext(), SigninActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast toast = Toast.makeText(activityContext,"OPS ACONTECEU ALGUM ERRO...", Toast.LENGTH_SHORT);
                            toast.show();
                        }
                    }
                });
    }

}

SignUpActivity.class:

package com.hotelaria.neoris.checkincheckout.activitys;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.hotelaria.neoris.checkincheckout.R;
import com.hotelaria.neoris.checkincheckout.models.objects.Firebase;

public class SignupActivity extends AppCompatActivity {

    private Firebase mFirebase;

    private EditText mEditText;
    private ImageButton mImageButton;
    private Button mButton;

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

        this.onClickButtonBackToLoginActivity();
        this.onClickButtonRegisterUser();
    }

    private void onClickButtonBackToLoginActivity(){
        this.mImageButton = findViewById(R.id.imageButtonBackToLoginActivity);
        this.mImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SignupActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void onClickButtonRegisterUser(){
        this.mButton = findViewById(R.id.buttonRegisterUser);
        this.mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context activityContext = getApplicationContext();

                if(checkEditText() == true) {
                    Toast toast = Toast.makeText(activityContext, "PREENCHA O E-MAIL E A SENHA !!", Toast.LENGTH_SHORT);
                    toast.show();
                } else {
                    mFirebase = new Firebase();
                    mFirebase.writeNewUser(SignupActivity.this, activityContext, getEmail(), getPassword());
                }
            }
        });
    }

    private Boolean checkEditText(){
        if(getEmail().isEmpty() || getPassword().isEmpty()){
            return true;
        } else {
            return false;
        }
    }

    private String getEmail(){
        this.mEditText = findViewById(R.id.editTextEmail);
        return this.mEditText.getText().toString();
    }

    private String getPassword(){
        this.mEditText = findViewById(R.id.editTextPassword);
        return this.mEditText.getText().toString();
    }



}

SignInActivity.class

package com.hotelaria.neoris.checkincheckout.activitys;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.hotelaria.neoris.checkincheckout.R;

public class SigninActivity extends AppCompatActivity {

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

        this.onClickButtonBackToLoginActivity();
        this.onClickButtonGoToMainActivity();
    }

    public void onClickButtonBackToLoginActivity(){
        ImageButton imageBtnSignInActivity = findViewById(R.id.imageButtonBackToLoginActivity);
        imageBtnSignInActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SigninActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    public void onClickButtonGoToMainActivity(){
        Button imageBtnSignInActivity = findViewById(R.id.buttonRegisterUsr);
        imageBtnSignInActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SigninActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}
    
asked by anonymous 25.05.2018 / 02:01

1 answer

0

I would advise you to use these methods within your activity yourself. Try changing this method from the Firebase.class class

public void writeNewUser(final Activity activity, final Context activityContext, String email, String password){
        this.mFirebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()) {
                            Toast.makeText(activityContext,"SUCESSO, CONTA CADASTRADA!", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(activityContext, SigninActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(activityContext,"OPS ACONTECEU ALGUM ERRO...", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
}

Within Activity you are inserting onClick methods but you are not referencing a button. This can only be done if within the xml of this activity has the method android:onClick="nomeMetodo" and this method has a View as in this code.
Within SignInActivity.class put the following code:

package com.hotelaria.neoris.checkincheckout.activitys;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.hotelaria.neoris.checkincheckout.R;

public class SigninActivity extends AppCompatActivity {

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

        Button btn1 = (Button) findViewById(R.id.buttonId1);
        Button btn2 = (Button) findViewById(R.id.buttonId2);

        btn1.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                Intent intent = new Intent(SigninActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
             }
         });

         btn2.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                Intent intent = new Intent(SigninActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
             }
         });
    }

}

If you want to put some image in your button put the image in the folder drawable and in the android:background attribute put the path of this image. Following:

<Button
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/imagem" />

And keep this code model for the other SignUpActivity.class activity.

    
28.05.2018 / 14:26