gpt4 book ai didi

java - Android Facebook LoginButton onActivityResult() 空对象引用

转载 作者:行者123 更新时间:2023-11-30 00:38:19 29 4
gpt4 key购买 nike

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

异常:

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) 

主要 Activity .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

最佳答案

你的成员变量中已经有callbackManager了。
请在 onCreate 中进行以下更改

callbackManager = CallbackManager.Factory.create();
info = (TextView)findViewById(R.id.info);
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile", "email", "user_friends");

关于java - Android Facebook LoginButton onActivityResult() 空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43022951/

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