9

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
BBdev
  • 4,898
  • 2
  • 31
  • 45
Nathan30
  • 689
  • 2
  • 8
  • 29

5 Answers5

22

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.
Community
  • 1
  • 1
Pablo Pantaleon
  • 1,506
  • 1
  • 19
  • 38
  • But it's very annoying the render problems, there is no solution for this ? – Nathan30 Mar 26 '15 at 10:23
  • 1
    I'm not sure right now how i can avoid this... because you're right is very annoying. If i find a way, i will update the answer. – Pablo Pantaleon Mar 26 '15 at 20:28
  • This actually makes a lot of sense. I had the exact same issue. Cheers. I reckon the new Facebook SDK will have some render elements... The same happens with Google+ – Deegriz Mar 30 '15 at 18:11
3

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

Esseme
  • 179
  • 1
  • 12
2

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'
Barakuda
  • 790
  • 8
  • 16
1

For those who have this problem of rendering, I find a solution. I don't use anymore the gradle.build dependencies to the facebook SDK. I download the SDK from the website of Facebook, and I import the module into my project in Android Studio. Then, add

<com.facebook.widget.LoginButton
    android:id="@+id/authButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="45dp" />

to your layout xml

Nathan30
  • 689
  • 2
  • 8
  • 29
-2

I tried this and it works

Added this in dependencies

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

enter image description here

   public class MainActivity extends Activity {
        CallbackManager callbackManager;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            FacebookSdk.sdkInitialize(getApplicationContext());
            callbackManager = CallbackManager.Factory.create();
            setContentView(R.layout.activity_main);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);


            return true;
            }
        }

enter image description here

Cristiana Chavez
  • 11,349
  • 5
  • 55
  • 54