Facebook Sdk Has Not Been Initialized FacebookSdk.sdkInitialize()
AndroidReact NativeFacebook LoginAndroid FacebookFbsdkAndroid Problem Overview
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.
What I've tried:
I had tried most of the things found on Internet but did not get anything regarding this.
Here is my 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:
<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:
05-13 16:30:39.332: E/AndroidRuntime(10264): Caused by: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.
Android Solutions
Solution 1 - Android
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:
https://i.stack.imgur.com/ENLN4l.png" width="400" />
I learned that FacebookSdk.sdkInitialize
is deprecated. see here
After some searching, I realized that the guide did not contain the steps to add the Facebook App ID for my app.
Solution
-
Open
android/app/src/main/AndroidManifest.xml
file and look in the<application>
tag to confirm that thismeta-data
tag exists:<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
-
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:<string name="facebook_app_id">YOUR_APP_ID_HERE</string>
-
Run
react-native run-android
.
These are the steps that worked for me.
Solution 2 - Android
You have to use FacebookSdk.sdkInitialize(getApplicationContext());
before setContentView(R.layout.activity_main);
as documentation states out. In case you need a complete facebook login example, check this one here.
Solution 3 - Android
You don't need to use FacebookSdk.sdkInitialize anymore. Check if your:
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
is inside <application>
tag.
Solution 4 - Android
There is a reason why sdkInitialize()
is deprecated.
Go to your manifest file within the android folder and add following
<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:
<string name="facebook_app_id">APP_ID</string>
Close your Metro Builder and rebuild your Project using react-native run-android
Solution 5 - Android
Follow only 2 Step and your Facebook Sdk iw working in 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
FacebookSdk.sdkInitialize(getApplicationContext());
Solution 6 - Android
After checking the documentation I found that they are asking to initialize FacebookSdk in Application class onCreate()
Method.
Snap code from Facebook doc:
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);
}
}
Solution 7 - Android
Seeing the responses listed in this question, the old way to initialize Facebook was:
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()
FacebookSdk.sdkInitialize()
now is deprecated.
Now (2021) Facebook initialization has changed, the Facebook initialization is automatically.
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:
implementation 'com.facebook.android:facebook-android-sdk:latest.release'
Add Your Facebook App ID and Client Token
Add your Facebook App ID and Client Token to your project's strings file and update your Android manifest:
1 Open your /app/res/values/strings.xml
file.
2 Add a string element with the name attribute facebook_app_id and value as your Facebook App ID to the file. 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
4 Add a uses-permission element to the manifest:
<uses-permission android:name="android.permission.INTERNET"/>
5 Add a meta-data element to the application element:
<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>
Solution 8 - Android
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);
Solution 9 - Android
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. This is in android/app/build.gradle file
Solution 10 - Android
My app was crashing because the Privacy Policy url was not provided in "Settings/Basic" in https://developers.facebook.com/. (in fact it was provided but I had to "Save changes" again. Seems a bug from Facebook).
The app was working fine and the error message Facebook Sdk Has Not Been Initialized FacebookSdk.sdkInitialize()
disappeared. Hope this help.