Android Studio 1.1.0 Facebook SDK Login

女生的网名这么多〃 提交于 2019-12-04 00:32:35

问题


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

回答1:


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.



回答2:


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




回答3:


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'



回答4:


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




回答5:


I tried this and it works

Added this in dependencies

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

   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;
            }
        }



来源:https://stackoverflow.com/questions/29268738/android-studio-1-1-0-facebook-sdk-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!