在回调管理器上获取空指针异常
Posted
技术标签:
【中文标题】在回调管理器上获取空指针异常【英文标题】:Getting null pointer exception on callbackmanager 【发布时间】:2015-06-20 09:35:14 【问题描述】:我正在使用 Facebook SDK。记录时,我得到一个空指针异常,
LoginButton loginButton;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
callbackManager = CallbackManager.Factory.create();
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
loginButton = (LoginButton)findViewById(R.id.login_button);
List<String> permissionNeeds = Arrays.asList("user_photos", "email", "user_birthday", "public_profile");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
@Override
public void onSuccess(LoginResult loginResult)
System.out.println("onSuccess");
@Override
public void onCancel()
System.out.println("onCancel");
@Override
public void onError(FacebookException exception)
Log.v("LoginActivity", exception.getCause().toString());
);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
错误是从第二行的方法 onActivityResult 引发的, callbackManager.onActivityResult(requestCode, resultCode, data);.
堆栈跟踪的一部分:
06-20 14:07:52.253 21310-21310/com.itspirits.fbchatdownloader E/androidRuntime﹕ FATAL EXCEPTION: main
Process: com.itspirits.fbchatdownloader, PID: 21310
java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=64206, result=-1, data=Intent (has extras) to activity com.itspirits.fbchatdownloader/com.itspirits.fbchatdownloader.MainActivity: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3365)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.itspirits.fbchatdownloader.MainActivity$1.onError(MainActivity.java:53)
at com.facebook.login.LoginManager.finishLogin(LoginManager.java:507)
at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:192)
at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:140)
at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:82)
at com.itspirits.fbchatdownloader.MainActivity.onActivityResult(MainActivity.java:69)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
随机思想移动callbackManager = CallbackManager.Factory.create();
下onCreate(...)
以前是这样的,我刚刚测试了一下,结果还是一样
我在 crashlytics 中看到过这种崩溃发生过一次,但无法重现。您能否提供有关如何重现崩溃的任何见解?我总是能够成功登录 facebook。
【参考方案1】:
这是我的 Facebook 登录代码。
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_login);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday, user_photos, user_friends"));
loginButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>()
private ProfileTracker mProfileTracker;
@Override
public void onSuccess(final LoginResult loginResult)
loginButton.setVisibility(View.INVISIBLE);
mProfileTracker = new ProfileTracker()
@Override
protected void onCurrentProfileChanged(Profile profile1, Profile profile)
final String userId = loginResult.getAccessToken().getUserId();
String profileImgUrl = "https://graph.facebook.com/" + userId + "/picture?type=large";
Log.d("Facebook ", "userId : " + userId);
final String token = loginResult.getAccessToken().getToken();
Log.d("Facebooklog ", "Token " + token);
if (profile == null) return;
String name = profile.getName();
;
mProfileTracker.startTracking();
@Override
public void onCancel()
Log.d("facebook - onCancel", "cancelled");
@Override
public void onError(FacebookException e)
Log.d("facebook - onError", e.getMessage());
);
);
希望对你有帮助
【讨论】:
【参考方案2】:callbackManager 是否为空? 数据是否为空? (不太可能)
您可以进行一些简单的调试,例如在使用前打印变量,以自己查看什么以及何时为 null :-)
【讨论】:
这是问题所在,data、requestCode 或 resultCode 都不是 null 嗯,有什么东西,是上面的完整堆栈吗? 添加了完整的堆栈跟踪 只是说,(我正在努力寻找问题所在 D:) 但我注意到第 3 行显示“who=null”:/ 还有你的代码所在的包是如何调用的?跨度> 我刚刚找到它,对不起,这是一个非常愚蠢的错误,我忘了添加 keyhashes,实际上,这是我使用 facebook sdk 的第一个 android 应用程序:/以上是关于在回调管理器上获取空指针异常的主要内容,如果未能解决你的问题,请参考以下文章