在我的菜单屏幕中,用户必须使用其Google帐户登录才能使用我的应用程序。成功登录后,用户将进入个人资料屏幕,其中将显示其姓名,电子邮件和Gmail个人资料图片。
在我的Profile类中,在onCreate
方法中,我有这段代码:
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);
基本上,它从Menu类中获取数据(名称,电子邮件和个人资料图片URL),并能够使用该数据在个人资料屏幕上设置用户名称,显示其电子邮件并显示其Gmail个人资料图片。首次登录时,一切正常。
但是,我的应用程序顶部有一个导航栏。假设用户转到游戏屏幕,然后返回到个人资料屏幕。该应用程序崩溃了,这就是我得到的:
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)
当我尝试返回个人资料屏幕时,为什么我的应用程序崩溃了?我相信在我使用getExtras()
和getString()
方法的地方都会引起问题。
编辑:在菜单活动中包括显示数据发送方式的方法。
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);
}
}
答案 0 :(得分:1)
尝试一下
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);
答案 1 :(得分:1)
首先,请先检查null
个值,然后再将其发送到intent.
检查日志,以获取是否所有值。
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();
}
}
}