In my menu screen, the user must log in using their Google account to use my app. Upon successfully signing in, the user will enter the profile screen, where their name, email, and Gmail profile picture will be displayed.
In my Profile class, inside the onCreate
method, I have this chunk of code:
Intent dataFromMain = getIntent();
String fullName = dataFromMain.getExtras().getString("name");
String user_email = dataFromMain.getExtras().getString("email");
String profile_pic = dataFromMain.getExtras().getString("img_url");
Name.setText(fullName);
Username.setText(user_email);
Glide.with(this).load(profile_pic).into(Prof_Pic);
Basically, it gets the data from the Menu class (name, email, and profile picture URL) and is able to use that data to set the name of the user on the profile screen, display their email, and show their Gmail profile picture. This all works fine when I first sign in.
However, I have a navigation bar at the top of my app. Let's say the user goes to the game screen and then goes back to the profile screen. The app crashes and this is what I get:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.debata.com.debata, PID: 11783
java.lang.RuntimeException: Unable to start activity ComponentInfo{app.debata.com.debata/app.debata.com.debata.Profile}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at app.debata.com.debata.Profile.onCreate(Profile.java:84)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Why is my app crashing when I try returning to the profiles screen? I believe where I use the getExtras()
and getString()
methods are causing the problem.
Edit: Including the method in the Menu activity that shows how data is being sent.
private void handleResult(GoogleSignInResult result) {
if(result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
String name = account.getDisplayName();
String email = account.getEmail();
String img_url = account.getPhotoUrl().toString();
Intent intent = new Intent(this, Profile.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("img_url", img_url);
startActivityForResult(intent, 1);
}
}