Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio 1.1.0 Facebook SDK Login

I try to test the login with facebook SDK.

So I add with compile 'com.facebook.android:facebook-android-sdk:4.0.0'.

Then add FacebookSdk.sdkInitialize(getApplicationContext()); to MainActivity.java

But when I add

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

I have this following error :

Rendering Problems The following classes could not be instantiated:
com.facebook.login.widget.LoginButton (Open Class, Show Exception)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE  
Exception Details java.lang.NoClassDefFoundError: Could not initialize class com.facebook.login.widget.LoginButton
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:806)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:385) Copy stack to clipboard
like image 666
Nathan30 Avatar asked Mar 25 '15 23:03

Nathan30


3 Answers

Simply specifying a different version of Facebook SDK worked for me. Adjust the build.graddle file dependencies part. Mine looks like;

dependencies {
compile 'com.facebook.android:facebook-android-sdk:4.4.0'}

Version 4.0.0 persistently threw the exception

I did not have to import the Facebook SDK as a module

I had to play around with the different versions of the SDK as available here

like image 126
Esseme Avatar answered Sep 18 '22 20:09

Esseme


You didn't posted your Activity code. But i think your code is like:

setContentView(R.layout.my_login_layout);
FacebookSdk.sdkInitialize(getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();

The problem is the order of the code. Ignore the "Render problems" in the layout, change the order of the code to this:

FacebookSdk.sdkInitialize(getApplicationContext());
mCallbackManager = CallbackManager.Factory.create(); // this line doesn't matter 
setContentView(R.layout.my_login_layout);

Use this code in OnCreate(...) { ... }

Avoid layout render problems

if you want to solve the render problems (layout preview) check the @Nathan30 answer (see below).

  1. Download Facebook SDK
  2. Import it as a Module to your project
  3. Then add <com.facebook.widget.LoginButton .../> to your layout.
like image 43
Pablo Pantaleon Avatar answered Sep 20 '22 20:09

Pablo Pantaleon


Simple, quick and easy: update the SDK version on Gradle -> Dependencies to the latest version and the rendering problem is solved. At the time of writing this, the latest Facebook SDK is 4.3.0, so this is what it should look like on Dependencies:

compile 'com.facebook.android:facebook-android-sdk:4.3.0'
like image 43
Barakuda Avatar answered Sep 20 '22 20:09

Barakuda