Android Facebook LoginButton onActivityResult空指针异常

时间:2013-11-20 05:14:06

标签: android facebook login nullpointerexception

我正在尝试为我的Android应用实施Facebook登录。我发布了我在这里写的测试代码。

public class MainActivity extends Activity {

private LoginButton loginButton;
private UiLifecycleHelper uiHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loginButton = (LoginButton) findViewById(R.id.facebookLogin);
    loginButton.setOnErrorListener(new ErrorHandler());
    StatusCallback callback = new CallBackHandler();
    loginButton.setSessionStatusCallback(callback);
    UiLifecycleHelper uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);

}

private class CallBackHandler implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state,
            Exception exception) {

        Log.d("facebook", "changing");

        Log.d("facebook state", state.toString());

        if (state.isOpened()) {
            Log.d("facebook", "facebook connected");
        }

        if (state.isClosed()) {
            Log.d("facebook", "facebook disconnected");
        }
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
}

当我使用登录按钮登录Facebook时,这会引发一些例外情况。完整的堆栈跟踪在这里:

 11-20 04:48:34.582: E/AndroidRuntime(2164): FATAL EXCEPTION: main
11-20 04:48:34.582: E/AndroidRuntime(2164): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {com.example.facebooklogin/com.example.facebooklogin.MainActivity}: java.lang.NullPointerException
11-20 04:48:34.582: E/AndroidRuntime(2164):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at android.os.Looper.loop(Looper.java:137)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at java.lang.reflect.Method.invokeNative(Native Method)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at java.lang.reflect.Method.invoke(Method.java:511)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at dalvik.system.NativeStart.main(Native Method)
11-20 04:48:34.582: E/AndroidRuntime(2164): Caused by: java.lang.NullPointerException
11-20 04:48:34.582: E/AndroidRuntime(2164):     at com.example.facebooklogin.MainActivity.onActivityResult(MainActivity.java:59)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
11-20 04:48:34.582: E/AndroidRuntime(2164):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
11-20 04:48:34.582: E/AndroidRuntime(2164):     ... 11 more

你认为我哪里错了?

1 个答案:

答案 0 :(得分:1)

您已在UiLifecycleHelper方法中再次定义onCreate,覆盖全局UiLifecycleHelper

在onCreate中,你只需要初始化UiLifeCyclehelper

 uiHelper = new UiLifecycleHelper(this, callback);

这样做

private LoginButton loginButton;
private UiLifecycleHelper uiHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loginButton = (LoginButton) findViewById(R.id.facebookLogin);
    loginButton.setOnErrorListener(new ErrorHandler());
    StatusCallback callback = new CallBackHandler();
    loginButton.setSessionStatusCallback(callback);
    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);

}