必须在 AndroidManifest.xml 中设置有效的 Facebook 应用程序 ID

A valid Facebook app id must be set in the AndroidManifest.xml

我正在尝试使用本机反应和 fbsdk 包装器 https://github.com/facebook/react-native-fbsdk 在 Android 上设置 fbsdk。我在 iOS 上工作,但我卡在 Android 上。我根据 github 自述文件执行了所有步骤。

MainApplication.java

@Override
  public void onCreate() {
    super.onCreate();
    FacebookSdk.sdkInitialize(getApplicationContext());
    SoLoader.init(this, /* native exopackage */ false);
  }

strings.xml

<resources>
    <string name="app_name">FacebookLogin</string>
    <string name="facebook_app_id">1343643482425305</string>
    <string name="fb_login_protocol_scheme">fb1343643482425305</string>
</resources>

AndroidManifest.xml

<meta-data android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id"/>

但是我从 LogCat

收到这个错误
11-22 06:57:13.236 7570-7570/? I/zygote: Not late-enabling -Xcheck:jni (already on)
11-22 06:57:13.244 7570-7570/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
11-22 06:57:13.361 7570-7570/com.facebooklogin I/FacebookInitProvider: Failed to auto initialize the Facebook SDK
                                                                       A valid Facebook app id must be set in the AndroidManifest.xml or set by calling FacebookSdk.setApplicationId before initializing the sdk.
                                                                           at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:276)
                                                                           at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:232)
                                                                           at com.facebook.internal.FacebookInitProvider.onCreate(FacebookInitProvider.java:20)
                                                                           at android.content.ContentProvider.attachInfo(ContentProvider.java:1919)
                                                                           at android.content.ContentProvider.attachInfo(ContentProvider.java:1894)
                                                                           at android.app.ActivityThread.installProvider(ActivityThread.java:6285)
                                                                           at android.app.ActivityThread.installContentProviders(ActivityThread.java:5851)
                                                                           at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5772)
                                                                           at android.app.ActivityThread.-wrap1(Unknown Source:0)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                           at android.os.Looper.loop(Looper.java:164)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
11-22 06:57:13.364 7570-7570/com.facebooklogin D/AndroidRuntime: Shutting down VM
11-22 06:57:13.365 7570-7570/com.facebooklogin E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: com.facebooklogin, PID: 7570
                                                                 java.lang.RuntimeException: Unable to create application com.facebooklogin.MainApplication: A valid Facebook app id must be set in the AndroidManifest.xml or set by calling FacebookSdk.setApplicationId before initializing the sdk.
                                                                     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5794)
                                                                     at android.app.ActivityThread.-wrap1(Unknown Source:0)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                     at android.os.Looper.loop(Looper.java:164)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                  Caused by: A valid Facebook app id must be set in the AndroidManifest.xml or set by calling FacebookSdk.setApplicationId before initializing the sdk.
                                                                     at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:276)
                                                                     at com.facebook.FacebookSdk.sdkInitialize(FacebookSdk.java:232)
                                                                     at com.facebooklogin.MainApplication.onCreate(MainApplication.java:53)
                                                                     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1118)
                                                                     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5791)
                                                                     at android.app.ActivityThread.-wrap1(Unknown Source:0) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                     at android.os.Looper.loop(Looper.java:164) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

在 build.gradle(Module:app) 文件中添加以下内容:

repositories {
    mavenCentral()
}

compile 'com.facebook.android:facebook-android-sdk:4.10.0'

现在在 AndroidManifest.xml 文件中添加以下内容:

 <activity android:name="com.facebook.FacebookActivity"
           android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
           android:theme="@android:style/Theme.Translucent.NoTitleBar"
           android:label="@string/app_name" />

在 activity_main.xml 文件中添加以下内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.demonuts.fblogin.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000"
    android:layout_marginLeft="10dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/text"/>

<com.facebook.login.widget.LoginButton
    android:id="@+id/btnfb"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

我的案例中的元数据位于应用程序范围之外。通常,它应该像这样位于您的清单中:

<application>
      ...
      <meta-data
                android:name="com.facebook.sdk.ApplicationId"
                android:value="@string/facebook_app_id" />
    </application>

尝试替换:

<string name="facebook_app_id">1343643482425305</string>

<string name="facebook_application_id">1343643482425305</string>

我在 Facebook 开发控制台中创建一个新项目时遇到同样的问题,然后所有配置都正确但没有在测试设备中安装 Facebook。

只需安装 Facebook 应用程序!!!!那么错误日志就消失了。