0

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);
        }
    }
Bert Hanz
  • 417
  • 1
  • 7
  • 16

2 Answers2

1

Try this

Intent dataFromMain = getIntent();
String fullName = dataFromMain.getStringExtra("name");
String user_email = dataFromMain.getStringExtra("email");
String profile_pic = dataFromMain.getStringExtra("img_url");

Name.setText(fullName);
Username.setText(user_email);
Glide.with(this).load(profile_pic).into(Prof_Pic);
Shubham Vala
  • 1,024
  • 7
  • 18
  • When I go back from the Game screen to the Menu screen, the profile picture, the email, and the user's full name are no longer visible. It disappears. – Bert Hanz Dec 24 '18 at 15:17
  • @BertHanz please check in log that you get the name and other filed from google signin result or its blank. – Shubham Vala Dec 24 '18 at 17:42
  • There's no null data upon entering the Profile screen the first time. Only when I try to refresh the Profile screen, or go to another screen and go back to the Profile screen is when the app crashes. – Bert Hanz Dec 24 '18 at 22:36
  • @BertHanz I have understand your problem but still i need clarification so can you add the code for your flow in question so, i can give proper solution. – Shubham Vala Dec 25 '18 at 11:08
1

At First, check null values before sending it to intent. Check log if you are getting all values or not.

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();

        Log.e("my_tag", "name = " + (!TextUtils.isEmpty(name) ? name : "")); // check name is null
        Log.e("my_tag", "email = " + (!TextUtils.isEmpty(email) ? email : "")); // check email is null
        Log.e("my_tag", "img_url = " + (!TextUtils.isEmpty(img_url) ? img_url : "")); // check img_url is null

        // Check null value first and then startactivity
        if (!TextUtils.isEmpty(name) &&
                !TextUtils.isEmpty(email) &&
                !TextUtils.isEmpty(img_url)) {
            Intent intent = new Intent(this, Profile.class);
            intent.putExtra("name", name);
            intent.putExtra("email", email);
            intent.putExtra("img_url", img_url);

            startActivityForResult(intent, 1);
        } else {
            Toast.makeText(getApplicationContext(), "One of the values getting null", Toast.LENGTH_SHORT).show();
        }
    }
}
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
  • I appreciate your help, but your answer doesn't work. I think what's happening is when I go from the Game screen to the Profile screen, it destroys the previous screen and starts fresh. That's why the app crashes, it's not able to get data from the Menu screen because it's been destroyed. What I need to do now is learn how to resume activity when going from the Game screen back to the Profiles screen. But how do I resume activity for multiple activities? – Bert Hanz Dec 25 '18 at 15:34
  • Okay. you should use [startActivityForResult](https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=2ahUKEwjBire-4bzfAhULcCsKHV0MCWcQFjABegQICRAB&url=https%3A%2F%2Fdeveloper.android.com%2Ftraining%2Fbasics%2Fintents%2Fresult&usg=AOvVaw2zwI_xW8Y-TmYmDixc1HKP) for it. Have a look at [this question.](https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=2ahUKEwjBire-4bzfAhULcCsKHV0MCWcQFjAAegQIChAB&url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F10407159%2Fhow-to-manage-startactivityforresult-on-android&usg=AOvVaw3zKJv7-tfz4Kqjqht4O9KY) – Rumit Patel Dec 26 '18 at 05:30