web-dev-qa-db-fra.com

Erreur lors du gonflement de la classe com.facebook.widget.LoginButton avec Facebook SDK 4.0.1

Je sais que c'est une question très courante, mais j'ai lu toutes les questions de débordement de pile sans trouver de réponse.

J'essaie d'intégrer le bouton de connexion Facebook à l'aide du SDK Facebook 4.0.1 ajouté via Gradle, mais j'obtiens l'erreur suivante.

  Java.lang.RuntimeException: Unable to start activity ComponentInfo{se.speedle.Android/com.bryderi.speedle.Android.activity.MainActivity}: Android.view.InflateException: Binary XML file line #26: Error inflating class com.facebook.widget.LoginButton
        at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2211)
        at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2261)
        at Android.app.ActivityThread.access$600(ActivityThread.Java:141)
        at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1256)
        at Android.os.Handler.dispatchMessage(Handler.Java:99)
        at Android.os.Looper.loop(Looper.Java:137)
        at Android.app.ActivityThread.main(ActivityThread.Java:5103)
        at Java.lang.reflect.Method.invokeNative(Native Method)
        at Java.lang.reflect.Method.invoke(Method.Java:525)
        at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:737)
        at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:553)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: Android.view.InflateException: Binary XML file line #26: Error inflating class com.facebook.widget.LoginButton
        at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:707)
        at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:755)
        at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:758)
        at Android.view.LayoutInflater.inflate(LayoutInflater.Java:492)
        at Android.view.LayoutInflater.inflate(LayoutInflater.Java:397)
        at Android.view.LayoutInflater.inflate(LayoutInflater.Java:353)
        at Android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.Java:228)
        at Android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.Java:102)
        at com.bryderi.speedle.Android.activity.MainActivity.onCreate(MainActivity.Java:36)
        at Android.app.Activity.performCreate(Activity.Java:5133)
        at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1087)
        at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2175)
            at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2261)
            at Android.app.ActivityThread.access$600(ActivityThread.Java:141)
            at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1256)
            at Android.os.Handler.dispatchMessage(Handler.Java:99)
            at Android.os.Looper.loop(Looper.Java:137)
            at Android.app.ActivityThread.main(ActivityThread.Java:5103)
            at Java.lang.reflect.Method.invokeNative(Native Method)
            at Java.lang.reflect.Method.invoke(Method.Java:525)
            at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:737)
            at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:553)
            at dalvik.system.NativeStart.main(Native Method)
 Caused by: Java.lang.ClassNotFoundException: Didn't find class "com.facebook.widget.LoginButton" on path: DexPathList[[Zip file "/data/app/se.speedle.Android-1.apk"],nativeLibraryDirectories=[/data/app-lib/se.speedle.Android-1, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.Java:53)
        at Java.lang.ClassLoader.loadClass(ClassLoader.Java:501)
        at Java.lang.ClassLoader.loadClass(ClassLoader.Java:461)
        at Android.view.LayoutInflater.createView(LayoutInflater.Java:559)
        at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:696)
            at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:755)
            at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:758)
            at Android.view.LayoutInflater.inflate(LayoutInflater.Java:492)
            at Android.view.LayoutInflater.inflate(LayoutInflater.Java:397)
            at Android.view.LayoutInflater.inflate(LayoutInflater.Java:353)
            at Android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.Java:228)
            at Android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.Java:102)
            at com.bryderi.speedle.Android.activity.MainActivity.onCreate(MainActivity.Java:36)
            at Android.app.Activity.performCreate(Activity.Java:5133)
            at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1087)
            at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2175)
            at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2261)
            at Android.app.ActivityThread.access$600(ActivityThread.Java:141)
            at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1256)
            at Android.os.Handler.dispatchMessage(Handler.Java:99)
            at Android.os.Looper.loop(Looper.Java:137)
            at Android.app.ActivityThread.main(ActivityThread.Java:5103)
            at Java.lang.reflect.Method.invokeNative(Native Method)
            at Java.lang.reflect.Method.invoke(Method.Java:525)

Je peux voir la classe LoginButton dans Android Studio, donc ça fonctionne

Voici mes dépendances build.gradle

dependencies {

aspects 'org.robobinding:robobinding:0.8.9'
compile 'org.springframework.Android:spring-Android-rest-template:2.0.0.M1'
compile 'com.Android.support:appcompat-v7:21.0.3'
compile 'com.Android.support:support-v4:21.0.3'
compile 'com.Android.support:support-v13:21.0.3'

compile 'com.Android.support:recyclerview-v7:21.0.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.3.2'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.facebook.Android:facebook-Android-sdk:4.0.1'
compile 'me.alexrs:recyclerview-renderers:1.0.3'
compile 'com.google.Android.gms:play-services:6.5.87'
}

Voici ma mise en page

<?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.widget.LoginButton
        Android:id="@+id/connectWithFbButton"
        style="@style/com_facebook_loginview_default_style"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_centerInParent="true"
        Android:layout_gravity="center_horizontal"
        Android:text="  connect_with_facebook"/>
</LinearLayout>

Et les parties pertinentes de mon activité

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getApplicationContext());


        callbackManager = CallbackManager.Factory.create();


        setContentView(R.layout.activity_main);

        // Set a Toolbar to replace the ActionBar.
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Find our drawer view
        dlDrawer = (FragmentNavigationDrawer) findViewById(R.id.drawer_layout);
        // Setup drawer view
        dlDrawer.setupDrawerConfiguration((ListView) findViewById(R.id.lvDrawer), toolbar,
                R.layout.drawer_nav_item, R.id.flContent);
        // Add nav items
        dlDrawer.addNavItem("Home", "First Fragment", ClassifiedsFragment.class);
        dlDrawer.addNavItem("Favourites", "Favourites", ClassifiedsFragment.class);
        dlDrawer.addNavItem("My Items", "Items", ClassifiedsFragment.class);
        dlDrawer.addNavItem("Settings", "Settings", ClassifiedsFragment.class);


        //dlDrawer.addNavItem("Second", "Second Fragment", SecondFragment.class);
        //dlDrawer.addNavItem("Third", "Third Fragment", ThirdFragment.class);
        // Select default
        if (savedInstanceState == null) {
            dlDrawer.selectDrawerItem(0);
        }
    }
32
Mattias Johansson

Dans facebook sdk 4.0.1, la classe LoginButton n'est pas à l'intérieur com.facebook.widget paquet. C'est à l'intérieur com.facebook.login.widget paquet. Votre déclaration xml devrait donc ressembler à ceci:

  <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" />
35
Mehul Joisar

Causé par: le SDK n'a pas été initialisé, assurez-vous d'appeler d'abord FacebookSdk.sdkInitialize ().

Assurez-vous d'avoir initialisé le SDK facebook avant setContentView.

FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
165
Lay Leangsros

Ajoutez uniquement ces méthodes de la classe Application de votre application ::

FacebookSdk.sdkInitialize(getApplicationContext());

AppEventsLogger.activateApp(this);

Befeore: setContentView dans onCreate Activity.

Dans l'importation de dépendances:

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

Prendre plaisir :)

10
Krste Moskov

hé amis, tout votre code est correct. suivez les instructions ci-dessous.

mettre FacebookSdk.sdkInitialize (getApplicationContext ()); avant setContentView ();

et mettez la ligne suivante dans votre disposition parent xml

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

comme,

<?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>
5
Amol Suryawanshi

Assurez-vous que votre SDK Facebook est initialisé avant setContentView FacebookSdk.sdkInitialize(getApplicationContext()); setContentView(R.layout.activity_main);

5
Zahid Ali

Le SDK n'a pas été initialisé, assurez-vous d'appeler FacebookSdk.sdkInitialize () en premier

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

Sur les documents Facebook, le package est obsolète. Pour les nouvelles versions, 4 et plus, le nouveau package du bouton est com.facebook.login.widget.LoginFacebook.

dépendances:

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" />

Prendre plaisir!

1
Marcio Mangar

FacebookSdk.sdkInitialize (); est obsolète. Ne l'utilisez plus.

Et suivez ce tutoriel ici: https://developers.facebook.com/docs/facebook-login/Android sans utiliser le com.facebook.login.widget.LoginButton.

Essayez d'utiliser un bouton normal en utilisant ce 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);
        }
    }
0
André Luiz Reis