Facebook SDK Android登录按钮错误

时间:2015-11-08 23:35:19

标签: java android xml facebook

我是Android应用程序开发的新手,并尝试使用Facebook登录按钮创建一个简单的活动。但是,当我在手机上部署应用程序时,出现以下错误:

11-09 04:48:44.260 24890-25085/com.example.fblogin E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #5
11-09 04:48:44.260 24890-25085/com.example.fblogin E/AndroidRuntime: Process: com.example.fblogin, PID: 24890
11-09 04:48:44.260 24890-25085/com.example.fblogin E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
11-09 04:48:44.260 24890-25085/com.example.fblogin E/AndroidRuntime:     at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
11-09 04:48:44.260 24890-25085/com.example.fblogin E/AndroidRuntime:     at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:774)
11-09 04:48:44.260 24890-25085/com.example.fblogin E/AndroidRuntime:     at com.facebook.internal.Utility.queryAppSettings(Utility.java:822)
11-09 04:48:44.260 24890-25085/com.example.fblogin E/AndroidRuntime:     at com.facebook.login.widget.LoginButton$1.run(LoginButton.java:489)
11-09 04:48:44.260 24890-25085/com.example.fblogin E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-09 04:48:44.260 24890-25085/com.example.fblogin E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-09 04:48:44.260 24890-25085/com.example.fblogin E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)

我写了以下.xml和.java代码:

JAVA:

public class LoginActivity extends AppCompatActivity {

private LoginButton loginButton;
private CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    setContentView(R.layout.activity_login);
    loginButton = (LoginButton)findViewById(R.id.fb_login_button);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {

        }
    });
}

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

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="20dp" >

<com.facebook.login.widget.LoginButton
    android:id="@+id/fb_login_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
     />

我还在AndroidManifest.xml中添加了标签。任何有关如何使FB登录工作的帮助非常感谢。 Facebook文档有点不太明白。

1 个答案:

答案 0 :(得分:0)

  1. 尝试为登录按钮添加权限(在注册回调之前),例如:

    loginButton.setReadPermissions( “public_profile”);

  2. 在onActivityResult内部尝试添加

    super.onActivityResult(requestCode,resultCode,data);

  3. 之前的callbackManager.onActivityResult方法