How to open an Activity from a java class?

Asked

Viewed 199 times

0

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

I’m authenticating a new user in firebase, so far so good.. The problem is that I can not pass to another Activity, is 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();
            }
        });
    }
}
  • Why don’t you authenticate in a blank Activity that from there td is authenticated, if there is a user, it goes to the main part, if not, it goes to the login screen? I usually do this, create a blank Activity with name start, the first to be started is it, if there is user goes to the main screen, if not, goes to login screen

  • @Wotonsampaio could give me an example in code for kindness?

  • Ah, agr saw right your code, instead of activity.getApplicationContext() uses the very context that you went through, the activityContext along that line: Intent intent = new Intent(activity.getApplicationContext()...

1 answer

0

I advise you to use these methods within your Activity even.
Try changing this class method Firebase.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 the Activity you are inserting the methods onClick but is not referencing a button. This can be done only if within the xml of this Activity has the method android:onClick="nomeMetodo" and this method has as a parameter a View as in this code.
Within the 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 attribute android:background place the path of this image. Follow:

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

And keep this code template for the other Activity Signupactivity.class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.