Issue
I'm trying to make an app because of work at school but I can't solve this error (Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener(android.view.View$OnClickListener) )' on a null object reference at com.grupo1.denu1.RegisterActivity.onCreate(RegisterActivity.java:40), it occurs every time I try to open the registration activity, so I hope someone can help me
my code
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView register;
private EditText editTextEmail, editTextPassword;
private Button signIn;
private FirebaseAuth mAuth;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
register= (TextView) findViewById(R.id.register);
register.setOnClickListener (this);
signIn = (Button) findViewById(R.id.singIn);
signIn.setOnClickListener(this);
editTextEmail = (EditText) findViewById(R.id.email);
editTextPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
mAuth = FirebaseAuth.getInstance();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.register:
startActivity(new Intent(this, RegisterActivity.class));
break;
case R.id.singIn:
userLogin();
break;
}
}
private void userLogin() {
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if (email.isEmpty()){
editTextEmail.setError("Insira o Email!");
editTextEmail.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
editTextEmail.setError("Coloque um email válido!");
editTextEmail.requestFocus();
return;
}
if (password.isEmpty()){
editTextPassword.setError("Insira a senha!");
editTextPassword.requestFocus();
return;
}
if (password.length() < 6){
editTextPassword.setError("Senha precisa ter no minimo 6 caracteres!");
editTextPassword.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
startActivity(new Intent(MainActivity.this, MenuBase.class));
}else{
Toast.makeText(MainActivity.this, "Falha no Login! Tente novamente",Toast.LENGTH_LONG).show();
}
}
});
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<TextView
android:id="@+id/banner"
android:layout_width="285dp"
android:layout_height="45dp"
android:text="Denu"
android:textSize="40dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.176" />
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email:"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.351" />
<Button
android:id="@+id/singIn"
android:layout_width="178dp"
android:layout_height="52dp"
android:text="Entrar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.572" />
<TextView
android:id="@+id/forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Esqueceu a Senha?"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.632" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Senha:"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.465" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.793" />
<TextView
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Registrar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.933" />
</androidx.constraintlayout.widget.ConstraintLayout>
RegisterActivity
package com.grupo1.denu1;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
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;
import com.google.firebase.database.FirebaseDatabase;
import java.util.regex.Pattern;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private TextView banner, registerUser;
private EditText editTextFullName, editTextAge, editTextEmail, editTextPassword;
private ProgressBar progressBar;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
banner = (TextView)findViewById(R.id.banner);
banner.setOnClickListener(this);
registerUser = (Button) findViewById(R.id.registerUser);
registerUser.setOnClickListener(this);
editTextFullName = (EditText) findViewById(R.id.fullName);
editTextAge = (EditText) findViewById(R.id.age);
editTextEmail = (EditText) findViewById(R.id.email);
editTextPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.banner:
startActivity(new Intent(this, MainActivity.class));
break;
case R.id.registerUser:
registerUser();
break;
}
}
private void registerUser() {
String email= editTextEmail.getText().toString().trim();
String password= editTextPassword.getText().toString().trim();
String fullName= editTextFullName.getText().toString().trim();
String age= editTextAge.getText().toString().trim();
if (fullName.isEmpty()){
editTextFullName.setError("Nome necessário!");
editTextFullName.requestFocus();
return;
}
if (age.isEmpty()){
editTextAge.setError("Idade necessária!");
editTextAge.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
editTextEmail.setError("Insira um email válido!");
editTextEmail.requestFocus();
return;
}
if (password.isEmpty()){
editTextPassword.setError("Senha necessária!");
editTextPassword.requestFocus();
return;
}
if (password.length() < 6){
editTextPassword.setError("Senha precisa ter no minimo 6 caracteres!");
editTextPassword.requestFocus();
return;
}
progressBar.setVisibility(View.VISIBLE);
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
User user = new User(fullName, age, email);
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText(RegisterActivity.this, "Usuário criado com sucesso!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
//next video!
}else {
Toast.makeText(RegisterActivity.this, "Falha ao se registrar! Tente novamente!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
});
}else {
Toast.makeText(RegisterActivity.this, "Falha ao se registrar!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
}
Solution
The solution is to move your textview to your register activity layout.
<TextView
android:id="@+id/banner"
android:layout_width="285dp"
android:layout_height="45dp"
android:text="Denu"
android:textSize="40dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.176" />
You have placed your banner textview in your main activity layout which is the root cause of your problem.
Answered By - Danish
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.