My application Keeps stopping after I add the Verify Email button. My Email Verification and Application is working correctly before I adding the verify Email Address button. I have included the Logcat and Source code of both xml and Java
Here is the Java code:
package com.dev.hospital;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity {
FirebaseAuth fAuth;
Button resendCode;
TextView verifyMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verifyMsg.findViewById(R.id.verifymsg);
resendCode.findViewById(R.id.resendcode);
fAuth = FirebaseAuth.getInstance();
FirebaseUser user = fAuth.getCurrentUser();
if(!user.isEmailVerified()) {
resendCode.setVisibility(View.VISIBLE);
verifyMsg.setVisibility(View.VISIBLE);
resendCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseUser users = fAuth.getCurrentUser();
users.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this,"Verification Email Has Been Sent",Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("TAGS","onFailure: Email not sent" + e.getMessage());
}
});
}
});
}
}
//Logout function
public void logout(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(),login.class));
finish();
} }
It shows the warning: Null pointer exception in the module isEmailVerified()
Here is my XML code
<?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"
android:background="@color/backgroundmain"
android:clickable="false"
tools:context=".MainActivity">
<TextView
android:onClick="logout"
android:id="@+id/logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/font"
android:text="@string/logout"
android:textColor="@color/textcolormain"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.898"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.052" />
<TextView
android:id="@+id/verifymsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/font"
android:text="Email not Verified!"
android:textColor="#ff0000"
android:textSize="25sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.888" />
<Button
android:id="@+id/resendcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shapes"
android:fontFamily="@font/font"
android:text="Verify Now"
android:textColor="@color/textColor"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.535"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.976" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my Logcat: Shows error in onCreate: MainActivity.Java:24
2020-05-06 21:12:56.156 4930-4930/com.dev.hospital E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dev.hospital, PID: 4930
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.hospital/com.dev.hospital.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2725)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2790)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6261)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1064)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference
at com.dev.hospital.MainActivity.onCreate**(MainActivity.java:24)**
at android.app.Activity.performCreate(Activity.java:6705)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2790)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6261)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1064)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)