72

I've been following the following tutorial to integrate my app with Facebook. Facebook tutorial

I've followed everything on the tutorial, but I've been getting applicationId cannot be null in two cases, and it's really frustrating.

My FacebookActivity onCreate has the following, which is exactly the same as the tutorial:

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);
    setContentView(R.layout.main_fb);

    FragmentManager fm = getSupportFragmentManager();
    fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
    fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);

    FragmentTransaction transaction = fm.beginTransaction();
    for(int i = 0; i < fragments.length; i++) 
    {
        transaction.hide(fragments[i]);
    }
    transaction.commit();
}

However when I try to display the activity I get applicationId cannot be null, and the line LogCat points me to is: uiHelper.onCreate(savedInstanceState);

So then I tried commenting out that line, and the activity is displayed. However now when I click on the LoginButton, I get the same error but this time is points me to the applicationId field in the LoginButton class from facebook.

I already have the Id in my string values and my manifest like this:

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

I tried getting the Id using code, but nothing changed.

What exactly is causing all this?

kabuko
  • 36,028
  • 10
  • 80
  • 93
Kakalokia
  • 3,191
  • 3
  • 24
  • 42

5 Answers5

225

TL;DR: you have to write your application's ID in your strings.xml and then reference (i.e. @strings/fb_app_id), because if you put it directly (as value) into AndroidManifest.xml it won't work.

you must define your applicationId in the AndroidManifest.xml like this:

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

under <application android:label="@string/app_name".... tag

where app_id is a string within your strings.xml.


sample:

 <application android:label="@string/app_name"
                 android:icon="@drawable/icon"
                 android:theme="@android:style/Theme.NoTitleBar"
            >
        <activity android:name=".HelloFacebookSampleActivity"
                  android:label="@string/app_name"
                  android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name="com.facebook.LoginActivity"
                  android:theme="@android:style/Theme.Translucent.NoTitleBar"
                  android:label="@string/app_name" />
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
    </application>

** please note <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/> is within <application> tag

-- and in strings.xml

<string name="app_id">1389xxxxxxxx</string>
Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
  • 46
    Apparently you HAVE to put it in the strings.xml, because if you reference it directly in AndroidManifest.xml it won't work. Kind of weird behaviour since that way works with Google's API's... – Mike Drakoulelis Jan 02 '14 at 19:55
  • 5
    @MikeDrakoulelis thats right, because if you reference it directly in AndroidManifest.xml it will parse it as `Integer`. – Mohammad Ersan Jan 12 '14 at 19:33
  • 16
    You can put it in the AndroidManifest.xml and avoid the conversion to an Integer by prefixing the string like so: `````` (note the space between the escape char & the ID string) – Mark Beaton Jun 07 '14 at 02:57
  • 1
    If the applicatiodId is reported as `null` by the Facebook sdk because of some error in the String resource parsing, you can manually set the applicationId after initializing the sdk. `FacebookSdk.sdkInitialize(getApplicationContext()); FacebookSdk.setApplicationId(getResources().getString(R.string.app_id)); mFacebookCallbackManager = CallbackManager.Factory.create();` – anj Jun 25 '15 at 09:46
  • I was having what @MarkBeaton and Mike said. Can you edit the answer to point that detail? made me waste half an hour. Summing up: You can't paste the applicationId in the manifest, you have to link it using a string resource, or add the initial separator as Mark said – voghDev Aug 29 '16 at 10:01
8

Since today the answer is not quite correct. If someone didn't use this: AppEventsLogger.activateApp(this);

Since last update you must do it or your app will crashed. And you also should pass Application here not Context

https://developers.facebook.com/docs/android/getting-started

// Add this to the header of your file:
import com.facebook.FacebookSdk;

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);
    }
}
Yura Buyaroff
  • 1,718
  • 3
  • 18
  • 31
  • 2
    What the heck. This just made my app crash. Is it me or is Facebook trying hard to spy on us? First they disable fetching messages from their graph api. Then they disable messages on the mobile browser unless you install their messenger app that needs 9271491724 permissions, now they force you to log your app with their events logger.. Am I being paranoid? – SudoPlz Jun 17 '16 at 00:12
  • When did they implemented adding this: AppEventsLogger.activateApp(this); Several days ago? Because yesterday I had no issues with that – Binev Jun 17 '16 at 11:45
  • @IvanBinev 30 minutes before my answer :) – Yura Buyaroff Jun 17 '16 at 13:51
  • super wired , but it is working, why FB are doing this crazy shit, you need accepted answer, this answer and the last FB sdk version to not have problems! – Stoycho Andreev Jul 06 '16 at 23:04
7

The problem is that the id is being converted to integer: https://code.google.com/p/android/issues/detail?id=78839

In my case the facebook_app_id was being set from the build.gradle file per flavor.

The solution was to wrap the id with ":

flavor.resValue "string", "facebook_app_id", "\"1111111111111\""

or if you would rather avoid escaping:

flavor.resValue "string", "facebook_app_id", '"1111111111111"'
maclir
  • 3,218
  • 26
  • 39
0

This little code modification at the activity helped me.

@Override
    protected void onCreate(Bundle savedInstanceState) {

        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(getApplication());

        super.onCreate(savedInstanceState);
        ...................
        ...................    
}
Smittey
  • 2,475
  • 10
  • 28
  • 35
Ahamed Mujeeb
  • 615
  • 1
  • 8
  • 13
-1

Actually you do not have to use flavor codes in gradle...

if you have number longer than 4 Bytes, you should this code in strings.xml

Note: Attention this quotation mark (")

<string name="facebook_app_id">"1111111111111"</string>
Kaloglu
  • 1,651
  • 1
  • 20
  • 31