1

I imported the Facebook SDK (v3.21.1) module into Android Studio (v1.0.2), and am trying to create a LoginActivity with com.facebook.widget.LoginButton as suggested in the documentation with the following code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.facebook.widget.LoginButton
        android:id="@+id/authButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        />

</LinearLayout>

However, the renderer in Android Studio says that it can't find the LoginButton class:

The following classes could not be found: - com.facebook.widget.LoginButton

I know for a fact that the class exists because I can open the file, and it is in the correct location:

clearly it exists

I know it exists, but to prove to Android Studio that it does, I tried creating a class, as suggested in the renderer, and receive the following error:

come on AS...

I've tried the following as well:

  • Re-importing the Facebook SDK (deleting/re-importing the Facebook module: deleting the physical files, etc)
  • Re-opening my project
  • Re-opening Android Studio
  • Building the project

Nothing works. What's ridiculous is that even the auto-complete can see the class:

auto-complete knows it

Edit: I tried just running the app in the emulator, ignoring the rendering error, and the app crashes with the following error:

12-31 03:47:15.593    2824-2824/com.test.playground E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.test.playground, PID: 2824
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.playground/com.test.playground.LoginActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.facebook.widget.LoginButton
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #7: 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.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
            at android.app.Activity.setContentView(Activity.java:1929)
            at com.test.playground.LoginActivity.onCreate(LoginActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            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/com.test.playground-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.test.playground-2, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
            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.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
            at android.app.Activity.setContentView(Activity.java:1929)
            at com.test.playground.LoginActivity.onCreate(LoginActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Again, the class does exist, it just seems that Android Studio can't find it. The class is viewable in the project directory under com.faceook.widget.LoginButton... I don't know what I'm missing. I've been trying to figure this out for hours and nothing is working.

Chris Cirefice
  • 5,475
  • 7
  • 45
  • 75
  • The answer on another thread: http://stackoverflow.com/questions/29735572/error-inflating-class-com-facebook-widget-loginbutton-with-facebook-sdk-4-0-1/32405244#32405244 – Marcio Mangar Sep 04 '15 at 19:30

2 Answers2

10

For those who still have problem, change com.facebook.widget.LoginButton to com.facebook.login.widget.LoginButton might help.

changbenny
  • 378
  • 6
  • 17
5

Have you integrated the Facebook SDK into your project? Check the build.gradle of your module and make sure that Facebook SDK is in dependencies. If not, add it and sync with Gradle and then try again.

The Facebook module should be added to the dependencies in build.gradle of the project that uses it:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':facebook')
}

As well, the Facebook module should be imported in settings.gradle:

include ':app', ':facebook'
Chris Cirefice
  • 5,475
  • 7
  • 45
  • 75
Hardik Chauhan
  • 2,750
  • 15
  • 30
  • You were right - I had included the `facebook` module in `settings.gradle`, but I hadn't actually listed it as a dependency in `build.gradle` for the project! *facepalm*. Anyway, would you mind if I clean up your answer with an edit to be more detailed (with the steps that I took)? I'd like to give you the accepted credit and the bounty but your answer currently looks more like a comment. For future readers who get stuck where I did, it would be helpful to have a full-fledged answer :) – Chris Cirefice Jan 03 '15 at 07:13
  • 1
    Sure man its nice share the solution you can edit it and make it better. – Hardik Chauhan Jan 03 '15 at 15:37