在回调管理器上获取空指针异常
Getting null pointer exception on callbackmanager
我正在使用 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.access00(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.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.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.access00(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 是否为空?
数据是否为空? (不太可能)
您可以进行一些简单的调试,例如在使用前打印变量,以便自己查看什么时候为空 :-)
这是我的 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());
}
});
}
});
}
希望对您有所帮助
我正在使用 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.access00(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.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.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.access00(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 是否为空? 数据是否为空? (不太可能)
您可以进行一些简单的调试,例如在使用前打印变量,以便自己查看什么时候为空 :-)
这是我的 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());
}
});
}
});
}
希望对您有所帮助