Nat*_*n30 9 sdk android facebook login
我尝试用facebook SDK测试登录.
所以我加上compile 'com.facebook.android:facebook-android-sdk:4.0.0'.
然后添加FacebookSdk.sdkInitialize(getApplicationContext());
到MainActivity.java
但是当我补充说
<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" />`
Run Code Online (Sandbox Code Playgroud)
我有以下错误:
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
Run Code Online (Sandbox Code Playgroud)
Pab*_*yes 22
您没有发布您的活动代码.但我认为你的代码是这样的:
setContentView(R.layout.my_login_layout);
FacebookSdk.sdkInitialize(getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
Run Code Online (Sandbox Code Playgroud)
问题是代码的顺序.忽略布局中的"渲染问题",将代码的顺序更改为:
FacebookSdk.sdkInitialize(getApplicationContext());
mCallbackManager = CallbackManager.Factory.create(); // this line doesn't matter
setContentView(R.layout.my_login_layout);
Run Code Online (Sandbox Code Playgroud)
在OnCreate中使用此代码(...){...}
避免布局渲染问题
如果你想解决渲染问题(布局预览),请查看@ Nathan30答案(见下文).
<com.facebook.widget.LoginButton .../>到您的布局.