Error inflating class com.facebook.widget.LoginButton with Facebook SDK 4.0.1

末鹿安然 提交于 2019-11-27 20:02:53

In facebook sdk 4.0.1, LoginButton class is not inside com.facebook.widget package. It is inside com.facebook.login.widget package. So your xml declaration should look like this:

  <com.facebook.login.widget.LoginButton
    android:id="@+id/connectWithFbButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal"
    android:text="  connect_with_facebook" />
Lay Leangsros

Caused by: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.

Make sure you initialized facebook SDK before setContentView.

FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);

Only add this methods of your app's Application class::

FacebookSdk.sdkInitialize(getApplicationContext());

AppEventsLogger.activateApp(this);

Befeore: setContentView in onCreate Activity.

In dependencies import:

compile 'com.facebook.android:facebook-android-sdk:[4,5)' or smaller

Enjoy :)

hey friends all your code is right. follow below instructions.

put FacebookSdk.sdkInitialize(getApplicationContext()); before setContentView();

and put following line in your xml parent layout

xmlns:facebook="http://schemas.android.com/apk/res-auto"

like,

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

    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="30dp"
        android:layout_marginTop="30dp" />


</RelativeLayout>

Make sure that your Facebook SDK is initialized before setContentView FacebookSdk.sdkInitialize(getApplicationContext()); setContentView(R.layout.activity_main);

The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this.getApplicationContext());            
    setContentView(R.layout.activity_main1);
}

On Facebook docs the package is outdated. For new versions, 4 and up the new package for the button is com.facebook.login.widget.LoginFacebook.

dependencies:

compile 'com.facebook.android:facebook-android-sdk:4.5.0'

activity_login.xml:

<com.facebook.login.widget.LoginButton
    android:id="@+id/connectWithFbButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal" />

Enjoy!

FacebookSdk.sdkInitialize(); is deprecated. Don't use it anymore.

And follow this tutorial here: https://developers.facebook.com/docs/facebook-login/android without using the com.facebook.login.widget.LoginButton.

Try to use a normal button using this code:

/**
 * Setup Facebook Login Button
 */
@Override
public void setupFacebookLoginButton() {
    Button btnFacebook = mActivityReference.findViewById(R.id.btnFacebook);
    Drawable leftDrawable = AppCompatResources.getDrawable(mActivityReference, R.drawable.ic_facebook_icon);
    btnFacebook.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, null, null);

    // Start Facebook integration
    LoginManager fbLoginManager = com.facebook.login.LoginManager.getInstance();
    mFaceCallbackManager = CallbackManager.Factory.create();
    fbLoginManager.registerCallback(mFaceCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            // Your accessToken is here
        }

        @Override
        public void onCancel() {
            Log.d("asd", "asd");
        }

        @Override
        public void onError(FacebookException e) {
            Log.d("asd", "asd");
        }
    });

    btnFacebook.setOnClickListener(v -> fbLoginManager.logInWithReadPermissions(
        mActivityReference,
        Arrays.asList("email", "public_profile", "user_birthday")));
}

/**
     * Facebook Call Back
     *
     * @param requestCode request code
     * @param resultCode  result code
     * @param data        data
     */
    @Override
    public void facebookCallBack(int requestCode, int resultCode, Intent data) {
        if (mFaceCallbackManager != null) {
            mFaceCallbackManager.onActivityResult(requestCode, resultCode, data);
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!