安卓系统,Facebook登录按钮引起崩溃(SDK 4.0.0)

4
在我的活动.xml文件中,我包含了Facebook登录按钮(来自文档)。
    <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" />

立即收到一个警告,提示

The following classes could not be found:
    - com.facebook.login.widget.LoginButton

文档中指出:

使用完整的类名 com.facebook.widget.LoginButton,在布局 XML 文件中添加按钮。

但修改顶部行并没有任何效果。应用程序立即崩溃,无法打开,并且在日志记录中没有任何内容(在外部设备上进行测试)。

如果移除该按钮,或将其更改为

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

这样做可以防止崩溃,但这是我第一次使用Facebook SDK,不确定普通按钮是否有效。

提前致谢。

1个回答

12
在充气视图之前添加FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
- 如果此xml在活动中使用,请在setContentView()之前编写上述行。
- 如果此xml在片段中使用,请在inflater.inflate...之前编写此行。

另外,只需忽略布局预览窗口中显示的警告。


我在Facebook登录方面遇到了一个新问题。我已经在这里发布了它,如果你有任何想法吗?谢谢。 - Birrel
好的回答,运行得很顺畅,谢谢@Ankit Bansal - ManishSB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接