简体   繁体   English

Android FacebookException:登录尝试失败

[英]Android FacebookException : Login attempt failed

I am using ParseFacebookUtils to login to my app from Facebook.我正在使用 ParseFacebookUtils 从 Facebook 登录我的应用程序。

LoginActivity's onCreate: LoginActivity 的 onCreate:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    loginButton = (Button)findViewById(R.id.loginButton);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {                  
            ParseFacebookUtils.logInWithReadPermissionsInBackground(LoginActivity.this,
                    Arrays.asList("public_profile", "email", "user_photos", "user_birthday"),
                    new LogInCallback() {
                        @Override
                        public void done(ParseUser user, ParseException e) {
                            if (user == null) {
                                Log.d(TAG, "Uh oh. The user cancelled the Facebook login.");
                            } else if (user.isNew()) {
                                Log.d(TAG, "User signed up and logged in through Facebook!");
                            } else {
                                Log.d(TAG, "User logged in through Facebook!");
                            }
                        }
                    }
            );
        }
    });
}

Application class onCreate:应用程序类 onCreate:

public void onCreate() {
    super.onCreate();

    Parse.initialize(this, app_id, client_key);
    FacebookSdk.sdkInitialize(getApplicationContext());
    ParseFacebookUtils.initialize(getApplicationContext());
}

Crash error:崩溃错误:

com.facebook.FacebookException: Log in attempt failed: LoginActivity could not be started
   at com.facebook.login.LoginManager.startLogin(LoginManager.java:382)
   at com.facebook.login.LoginManager.logInWithReadPermissions(LoginManager.java:262)
   at com.parse.FacebookAuthenticationProvider.authenticateAsync(FacebookAuthenticationProvider.java:150)
   at com.parse.ParseAuthenticationProvider.logInAsync(ParseAuthenticationProvider.java:57)
   at com.parse.ParseFacebookUtils.logInAsync(ParseFacebookUtils.java:259)
   at com.parse.ParseFacebookUtils.logInWithReadPermissionsInBackground(ParseFacebookUtils.java:155)
   at com.parse.ParseFacebookUtils.logInWithReadPermissionsInBackground(ParseFacebookUtils.java:167)
   at com.example.app.LoginActivity$1.onClick(LoginActivity.java:48)
   at android.view.View.performClick(View.java:4756)
   at android.view.View$PerformClick.run(View.java:19749)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5221)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

I keep getting the above crash when tapping on the login button.点击登录按钮时,我不断遇到上述崩溃。

Any ideas on how to fix this?有想法该怎么解决这个吗?

I have set the client id for Facebook and Parse in the strings.xml我已经在 strings.xml 中为 Facebook 和 Parse 设置了客户端 ID

UPDATE:更新:

Facebook SDK v4.0.1 Facebook SDK v4.0.1

Custom button onClick()自定义按钮 onClick()

@kRiZ : Yes.you forgot to add the com.facebook.FacebookActivity activity to AndroidManifest.xml . @kRiZ :是的。您忘记将com.facebook.FacebookActivity活动添加到AndroidManifest.xml

This Activity is a necessary part of the overall Facebook SDK , but is not meant to be used directly.此 Activity 是整个Facebook SDK的必要部分,但不能直接使用。 Add this Activity to your AndroidManifest.xml to ensure proper handling of Facebook SDK features.将此活动添加到您的AndroidManifest.xml以确保正确处理 Facebook SDK 功能。

Reference To use Facebook Login or Share, also add the FacebookActivity to the manifest:参考要使用 Facebook 登录或分享,还要将FacebookActivity添加到清单:

<activity android:name="com.facebook.FacebookActivity"
      android:configChanges=
             "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
      android:theme="@android:style/Theme.Translucent.NoTitleBar"
      android:label="@string/app_name" />

And

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

Where @string/facebook_app_id is your Facebook Application ID .其中@string/facebook_app_id是您的Facebook 应用程序 ID

This is how facebook authorization process will work.这就是facebook 授权流程的工作方式。

在此处输入图片说明

Fixed it by adding the following in AndroidManifest.xml:通过在 AndroidManifest.xml 中添加以下内容来修复它:

<activity android:name="com.facebook.FacebookActivity"
          android:theme="@android:style/Theme.Translucent.NoTitleBar"
          android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
          android:label="@string/app_name" />

According to this SO post , the above is needed in Facebook SDK 4.+.根据this SO post ,Facebook SDK 4.+ 中需要上述内容。

You need to register a callback for the button before you can use it:您需要先为按钮注册一个回调,然后才能使用它:

1.Declare a CallbackManager object: 1.声明一个CallbackManager对象:

 CallbackManager callbackManager;

2. In your onCreate method add this: 2. 在你的 onCreate 方法中添加:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onCreate(savedInstanceState);
  FacebookSdk.sdkInitialize(getApplicationContext());
  callbackManager = CallbackManager.Factory.create();
  setContentView(R.layout.activity_login);
  ...
}

3. Define onActivityResult and add this line: 3. 定义 onActivityResult 并添加以下行:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // Esto es necesario para el uso del botón de facebook
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM