简体   繁体   English

Facebook Sdk 尚未初始化 FacebookSdk.sdkInitialize()

[英]Facebook Sdk Has Not Been Initialized FacebookSdk.sdkInitialize()

Hey I know this was asked before, but none of the solutions seem to help.嘿,我知道以前有人问过这个问题,但似乎没有一个解决方案有帮助。 I'm using first time Facebook SDK in my application.我第一次在我的应用程序中使用 Facebook SDK。

What I've tried:我试过的:

I had tried most of the things found on Internet but did not get anything regarding this.我已经尝试了在Internet上找到的大部分内容,但没有得到任何关于此的信息。

Here is my MainActivity.java:这是我的 MainActivity.java:

 public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FacebookSdk.sdkInitialize(getApplicationContext());
   } 
}

Here is My Activitymain.xml:这是我的 Activitymain.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

 <com.facebook.login.widget.LoginButton
    android:id="@+id/connectWithFbButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal"
    android:text="  connect_with_facebook" /> 
</LinearLayout>

see my Logcat :看看我的Logcat

 05-13 16:30:39.332: E/AndroidRuntime(10264): Caused by: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.

Problem问题

While integrating Android SDK for a react-native project, I had finished the Android with React Native v0.30+ Project Configuration guide, and ran react-native run-android and then got this screen:在为 react-native 项目集成 Android SDK 时,我已经完成了Android with React Native v0.30+ 项目配置指南,然后运行react-native run-android然后得到这个屏幕:

I learned that FacebookSdk.sdkInitialize is deprecated.我了解到FacebookSdk.sdkInitialize已被弃用。 see here 看这里

After some searching, I realized that the guide did not contain the steps to add the Facebook App ID for my app.经过一番搜索,我意识到该指南没有包含为我的应用添加 Facebook 应用 ID 的步骤。

Solution解决方案

  1. Open android/app/src/main/AndroidManifest.xml file and look in the <application> tag to confirm that this meta-data tag exists:打开android/app/src/main/AndroidManifest.xml文件并查看<application>标记以确认此meta-data标记存在:

     <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
  2. Open android/app/src/main/res/values/strings.xml file and confirm that this there is a "facebook_app_id" string tag with your app id as the value:打开android/app/src/main/res/values/strings.xml文件并确认有一个“facebook_app_id”字符串标签,其中您的应用程序 ID 作为值:

     <string name="facebook_app_id">YOUR_APP_ID_HERE</string>
  3. Run react-native run-android .运行react-native run-android

These are the steps that worked for me.这些是对我有用的步骤。

You have to use FacebookSdk.sdkInitialize(getApplicationContext());你必须使用FacebookSdk.sdkInitialize(getApplicationContext()); before setContentView(R.layout.activity_main);setContentView(R.layout.activity_main); as documentation states out.正如 文件所述。 In case you need a complete facebook login example, check this one here .如果您需要完整的 facebook 登录示例, 请在此处查看此示例。

You don't need to use FacebookSdk.sdkInitialize anymore.您不再需要使用 FacebookSdk.sdkInitialize。 Check if your:检查您是否:

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

is inside <application> tag.<application>标签内。

There is a reason why sdkInitialize() is deprecated.不推荐使用sdkInitialize()是有原因的。

Go to your manifest file within the android folder and add following转到 android 文件夹中的清单文件并添加以下内容

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

After that append in your strings.xml file (res/values/strings.xml) the string entry:之后在您的strings.xml文件 (res/values/strings.xml) 中附加字符串条目:

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

Close your Metro Builder and rebuild your Project using react-native run-android关闭您的 Metro Builder 并使用 react-native run-android 重建您的项目

For SDK v13.+ you must add Apple ID and Client Token .对于 SDK v13.+,您必须添加Apple IDClient Token

  1. Open the /app/res/values/strings.xml file in your app project and add:在您的应用项目中打开 /app/res/values/strings.xml 文件并添加:
<string name="facebook_app_id">1234</string>
<string name="facebook_client_token">56789</string>
  1. Open the /app/manifests/AndroidManifest.xml file in your app project and add:在您的应用项目中打开 /app/manifests/AndroidManifest.xml 文件并添加:
<application android:label="@string/app_name" ...>
    ...
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    <meta-data android:name="com.facebook.sdk.ClientToken" android:value="@string/facebook_client_token"/>
    ...
</application>

You can find your Client Token in your FB Developer account:您可以在您的 FB 开发者帐户中找到您的客户令牌:

App > Dashboard > Settings > Advanced > Security > Client token.应用程序 > 仪表板 > 设置 > 高级 > 安全 > 客户端令牌。

Follow only 2 Step and your Facebook Sdk iw working in React Native只需 2 步,您的 Facebook Sdk iw 在 React Native 中工作

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

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

Don't Need this b'coz Its Deprecated Now不需要这个 b'coz 现在已弃用

  FacebookSdk.sdkInitialize(getApplicationContext());

After checking the documentation I found that they are asking to initialize FacebookSdk in Application class onCreate() Method .检查 文档后,我发现他们要求在 Application 类onCreate() Method 中初始化FacebookSdk

Snap code from Facebook doc:从 Facebook 文档获取代码:

  public class MyApplication extends Application {
 // Updated your class body:
 @Override
 public void onCreate() {
    super.onCreate();
    // Initialize the SDK before executing any other operations,
    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);
   }
 }

Seeing the responses listed in this question, the old way to initialize Facebook was:看到这个问题中列出的回复,初始化 Facebook 的旧方法是:

public class MyApplication extends Application {

 @Override
 public void onCreate() {
    super.onCreate();
    // Initialize the SDK before executing any other operations,
    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);
   }
 }

But we can get the message:但我们可以得到消息:

Facebook Sdk Has Not Been Initialized FacebookSdk.sdkInitialize() Facebook Sdk 尚未初始化 FacebookSdk.sdkInitialize()

FacebookSdk.sdkInitialize() now is deprecated. FacebookSdk.sdkInitialize()现在已弃用。

Now (2021) Facebook initialization has changed, the Facebook initialization is automatically .现在(2021)Facebook 初始化已经改变, Facebook 初始化是自动的

1 Add the following to the dependencies {} section of your build.gradle (module: app) file to compile the latest version of the Facebook SDK for Android: 1 将以下内容添加到 build.gradle (module: app) 文件的 dependencies {} 部分以编译最新版本的 Android 版 Facebook SDK:

implementation 'com.facebook.android:facebook-android-sdk:latest.release'

Add Your Facebook App ID and Client Token添加您的 Facebook 应用程序 ID 和客户端令牌

Add your Facebook App ID and Client Token to your project's strings file and update your Android manifest:将您的 Facebook 应用 ID 和客户端令牌添加到项目的字符串文件并更新您的 Android 清单:

1 Open your /app/res/values/strings.xml file. 1 打开/app/res/values/strings.xml文件。

2 Add a string element with the name attribute facebook_app_id and value as your Facebook App ID to the file. 2 将名称属性 facebook_app_id 和值作为您的 Facebook 应用程序 ID 的字符串元素添加到文件中。 For example例如

<string name="facebook_app_id">Facebook App ID</string>
<string name="facebook_client_token">Facebook Client ID</string>

3 Open /app/manifests/AndroidManifest.xml 3 打开/app/manifests/AndroidManifest.xml

4 Add a uses-permission element to the manifest: 4 将 uses-permission 元素添加到清单:

<uses-permission android:name="android.permission.INTERNET"/>

5 Add a meta-data element to the application element: 5 将元数据元素添加到应用程序元素:

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

Use Initialise Callback Constructor like this:像这样使用初始化回调构造函数:

    Handler mHandler = new Handler();
            FacebookSdk.InitializeCallback initializeCallback = new FacebookSdk.InitializeCallback() {
                @Override
                public void onInitialized() {

                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            //UI Code Here
                        }
                    });
                }
            };
    //before setContentView()   
 FacebookSdk.sdkInitialize(getActivity().getApplicationContext(),initializeCallback);

For Flutter Devs running into this issue.对于遇到此问题的 Flutter 开发人员。

Make sure you have followed the steps 1-6 as given in the facebook documentation .确保您已按照facebook 文档中给出的步骤 1-6 进行操作。

Adding FacebookSdk.sdkInitialize() in your MainActivity file won't fix the issue.在 MainActivity 文件中添加 FacebookSdk.sdkInitialize() 不会解决问题。

If this helps anyone.如果这对任何人都有帮助。 For me I had to make this change对我来说,我必须做出这个改变

implementation 'com.facebook.android:facebook-login:latest.release' // for FB login 

the above line of code must be added to the END of the dependencies object and not anywhere in between.上面的代码行必须添加到依赖对象的 END 中,而不是介于两者之间。 This is in android/app/build.gradle file这是在 android/app/build.gradle 文件中

For me, this issue arose due to a package in my application called react-native-fbsdk.对我来说,这个问题是由于我的应用程序中名为 react-native-fbsdk 的 package 引起的。 Please check your package.json file for the same.请检查您的package.json文件是否相同。

I removed the package using npm uninstall fbsdk and rebuilt by app and it worked perfectly.我使用npm uninstall fbsdk删除了 package 并由应用程序重建,它运行良好。

Hope this solution works for you!希望此解决方案对您有用!

remove android:exported=true from AndroidManifest.xml and targetSdkVersion should be less than 31从 AndroidManifest.xml 中删除android:exported=true并且 targetSdkVersion 应该小于 31

My app was crashing because the Privacy Policy url was not provided in "Settings/Basic" in https://developers.facebook.com/ .我的应用程序崩溃了,因为https://developers.facebook.com/的“设置/基本”中未提供隐私政策URL。 (in fact it was provided but I had to "Save changes" again. Seems a bug from Facebook). (实际上它是提供的,但我不得不再次“保存更改”。似乎是 Facebook 的一个错误)。

The app was working fine and the error message Facebook Sdk Has Not Been Initialized FacebookSdk.sdkInitialize() disappeared.该应用程序运行良好,错误消息Facebook Sdk Has Not Been Initialized FacebookSdk.sdkInitialize()消失了。 Hope this help.希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 facebook AppInviteContent:SDK尚未初始化,请务必先调用FacebookSdk.sdkInitialize() - facebook AppInviteContent: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first SDK尚未初始化,请务必先调用FacebookSdk.sdkInitialize() - The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first 集成Facebook Android SDK 4.4.0后,应用程序在FacebookSdk.SdkInitialize()上崩溃了吗? - App crashing at FacebookSdk.SdkInitialize() after integrating Facebook Android SDK 4.4.0? FacebookSdk.sdkInitialize(getApplicationContext())已弃用 - FacebookSdk.sdkInitialize(getApplicationContext()) Deprecated FacebookSdk.sdkInitialize (Context) 已弃用 - FacebookSdk.sdkInitialize (Context) is deprecated 有必要每次都调用FacebookSdk.sdkInitialize()吗? - It is necessary to call FacebookSdk.sdkInitialize() each time? FacebookSdk.sdkInitialize(getApplicationContext())方法不起作用 - FacebookSdk.sdkInitialize(getApplicationContext()) method not worked 错误:请确保先调用FacebookSdk.sdkInitialize() - Error: Make sure to call FacebookSdk.sdkInitialize() first FacebookSdk.sdkInitialize-调用一次,还是每次活动调用一次? - FacebookSdk.sdkInitialize - call it once, or once per activity? FacebookSdk.sdkInitialize 是在创建的每个活动上调用还是仅在 mainActivity 上调用 - is FacebookSdk.sdkInitialize called on every activity created or only on the mainActivity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM