gpt4 book ai didi

java - 在回调管理器上获取空指针异常

转载 作者:行者123 更新时间:2023-11-29 20:41:40 24 4
gpt4 key购买 nike

我正在使用 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 ResultInfo{who=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)

最佳答案

这是我的 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());
}
});
}
});
}

希望对你有帮助

关于java - 在回调管理器上获取空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30952462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com