Android Facebook LoginButton onActivityResult()null对象引用

时间:2017-03-25 23:00:06

标签: java android facebook nullpointerexception facebook-android-sdk

我想使用Facebook SDK loginButton对用户进行身份验证 我在https://code.tutsplus.com/tutorials/quick-tip-add-facebook-login-to-your-android-app--cms-23837上看了一个教程,我查看了facebook上的示例页面,但是我无法弄清楚为什么或者callbackManager.onActivityResult(...)返回NULL。注意我不想使用片段(我在示例中看到了其中的一些片段),因为我还没有理解它并希望保持简单。

Exeception:

27643-27643/com.example.android.helloworld E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.android.helloworld, PID: 27643

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} 
to activity {com.example.android.helloworld/com.example.android.helloworld.MainActivity}: 
java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.facebook.CallbackManager.onActivityResult(int, int, android.content.Intent)' on a null object reference
  at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
  at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
  at android.app.ActivityThread.-wrap20(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6119)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.facebook.CallbackManager.onActivityResult(int, int, android.content.Intent)' on a null object reference
  at com.example.android.helloworld.MainActivity.onActivityResult(MainActivity.java:55)
  at android.app.Activity.dispatchActivityResult(Activity.java:6932)
  at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
  at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) 
  at android.app.ActivityThread.-wrap20(ActivityThread.java) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:154) 
  at android.app.ActivityThread.main(ActivityThread.java:6119) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

MainActivity.java:

package com.example.android.helloworld;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

public class MainActivity extends AppCompatActivity {

    private CallbackManager callbackManager; //no assignment
    private LoginButton loginButton;
    private TextView info;

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

        CallbackManager callbackManager = CallbackManager.Factory.create();
        info = (TextView)findViewById(R.id.info);
        loginButton = (LoginButton)findViewById(R.id.login_button);
        loginButton.setReadPermissions("public_profile", "email", "user_friends");
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                info.setText(
                        "User ID: "
                                + loginResult.getAccessToken().getUserId()
                                + "\n" +
                                "Auth Token: "
                                + loginResult.getAccessToken().getToken()
                );
            }

            @Override
            public void onCancel() {
                info.setText("Login attempt canceled.");
            }

            @Override
            public void onError(FacebookException e) {
                info.setText("Login attempt failed.");
            }
        });
     }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/androidbg" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/info"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="18sp"
        />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        style="@style/FacebookLoginButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.android.helloworld">

    <uses-permission android:name="android.permission.INTERNET"/>
    <application android:allowBackup="true"
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.facebook.FacebookActivity"
            android:configChanges=
                "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@style/AppTheme"
            android:label="@string/app_name"
            tools:replace="android:theme"
            />
    </application>
</manifest>

facebook登录表格的图片:

Android Emulator - Pixel_XL_API_25:5554

1 个答案:

答案 0 :(得分:1)

你已经在成员变量中使用了callbackManager 请在onCreate中进行以下更改

RUN mkdir -p /root/.ssh && echo "$MY_PRIVATE_KEY" >/root/.ssh/id_rsa &&    chmod 0600 /root/.ssh/id_rsa &&    ssh-keyscan github.com >> /root/.ssh/known_hosts &&    cat /root/.ssh/known_hosts &&    ssh -vvv git@github.com &&    pip install git+ssh://git@github.com/my_project.git@v1.0.0 &&    rm /root/.ssh/id_rsa