Android Studio 1.1.0 Facebook SDK 登录

标签 android facebook authentication sdk

我尝试使用 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" />`

我有以下错误:

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

最佳答案

您没有发布您的 Activity 代码。但我认为你的代码是这样的:

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

问题是代码的顺序。忽略布局中的“渲染问题”,将代码的顺序更改为:

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

在 OnCreate(...) { ...

中使用此代码

避免布局渲染问题

如果你想解决渲染问题(布局预览)检查@Nathan30 answer (见下文)。

  1. 下载 Facebook SDK
  2. 将其作为模块导入您的项目
  3. 然后添加<com.facebook.widget.LoginButton .../>到你的布局。

关于Android Studio 1.1.0 Facebook SDK 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29268738/

相关文章:

android - 第一次启动TabActivity时,TabHost中的TabWidget可以不被选中吗?

java - Android xml和Java : Get text from Edit Text,找不到符号错误

java - 当 setMaxFileSize() 和 setMaxDuration() 组合使用时 MediaRecorder 服务器崩溃

Python-如何使用 Django 获取 Facebook 用户好友列表、状态更新等?

c# - 安全地验证客户端到服务器

xaml - 在 Windows 8 应用程序中将用户信息存储在哪里?

java - 如何使用 Picasso 上传和检索图像

ios - 有人得到了使用 Cocoapods 更新 'FacebookCore' pod 时出错的解决方法吗?

javascript - 通过 fb 登录网站时,我的函数 Test Api 不收集用户数据

ssl - jboss 5.1 ssl 客户端认证