Birrel Birrel - 3 months ago 22
Android Question

Android, Facebook Login Button Causes Crash (SDK 4.0.0)

In my activity

.xml
file, I include the Facebook Login Button (from this documentation)

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


Immediately I get a warning saying that

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


The documentation says to


Add the button to your layout XML file with the full class name,
com.facebook.widget.LoginButton


But changing that top line does nothing. The app crashes immediately, without opening, and nothing is recorded in the log cat (testing on external device).

Removing the button, or changing it to

<Button
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />


stops the crash, but this is my first time working with the Facebook SDK and don't know if the regular button will work.

Thank you in advance.

Answer

Add FacebookSdk.sdkInitialize(getActivity().getApplicationContext()); before inflating the view..
- If this xml is used in activity, write above line before setContentView()
- If this xml is used in fragment, write this line before inflater.inflate...

And, just ignore the warning shown in layout preview window