Android Facebook 应用程序ID不能为空。

72

我一直在按照以下教程将我的应用程序与Facebook集成: Facebook教程

我已经按照教程上的所有步骤操作,但在两种情况下都遇到了applicationId cannot be null错误,这真的很令人沮丧。

我的FacebookActivityonCreate代码如下,与教程中完全相同:

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();
}

然而,当我尝试显示该活动时,我收到了 applicationId cannot be null 的错误提示,并且LogCat指向的行是:uiHelper.onCreate(savedInstanceState);

然后我尝试注释掉那行代码,这样就可以显示该活动。然而,现在当我点击LoginButton时,我得到相同的错误,但这次它指向了Facebook的LoginButton类中的applicationId字段。

我已经在我的字符串值和清单中有这个ID:

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

我尝试使用代码获取Id,但是没有改变任何东西。

到底是什么导致了这一切?

5个回答

225
TL;DR: 在你的strings.xml中写入应用程序ID并引用它(即@strings/fb_app_id),因为如果你直接将其放入AndroidManifest.xml中作为值,它将无法正常工作。
你必须在AndroidManifest.xml中定义你的applicationId,如下所示: <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/> 放置在<application android:label="@string/app_name"....标签下面,
其中app_id是你的strings.xml中的一个字符串。
样例:
 <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>

请注意,<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/> 位于 <application> 标签内。
-- 并且在 strings.xml 文件中。
<string name="app_id">1389xxxxxxxx</string>

46
显然你必须将其放入strings.xml中,因为如果你直接在AndroidManifest.xml文件中引用它,它将无法工作。这种行为有点奇怪,因为在使用Google的API时,这种方式是可行的... - Mike Drakoulelis
5
没问题,请提供更多上下文,这样我才能给出准确的翻译。 - Mohammad Ersan
16
您可以将其放在AndroidManifest.xml中,并通过在字符串前添加前缀来避免将其转换为整数,例如:<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="\ XXXXXXXXXXXXXXXXX"/>(注意转义字符和ID字符串之间的空格)。 - Mark Beaton
1
如果由于字符串资源解析中的某些错误,Facebook sdk 报告 applicatiodIdnull,则您可以在初始化 sdk 后手动设置 applicationIdFacebookSdk.sdkInitialize(getApplicationContext()); FacebookSdk.setApplicationId(getResources().getString(R.string.app_id)); mFacebookCallbackManager = CallbackManager.Factory.create(); - anj
我正在关注@MarkBeaton和Mike所说的内容。你能否编辑答案并指出这个细节?这让我浪费了半个小时。总结:你不能在清单中粘贴applicationId,你必须使用字符串资源链接它,或者像Mark所说的那样添加初始分隔符。 - voghDev

8

从今天开始,答案不太正确。如果有人没有使用以下内容:

AppEventsLogger.activateApp(this);

自上次更新以来,您必须这样做,否则您的应用程序将崩溃。 并且您还应该在此处传递Application而不是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);
    }
}

2
这是什么鬼?这让我的应用程序崩溃了。是我还是Facebook在努力监视我们?首先,他们禁用了从他们的图形API中获取消息的功能。然后,除非您安装他们需要9271491724个权限的Messenger应用程序,否则他们会禁用移动浏览器上的消息,现在他们强制您使用他们的事件记录器登录应用程序...我是不是有点偏执狂? - SudoPlz
他们什么时候实现了添加这个:AppEventsLogger.activateApp(this);是几天前吗?因为昨天我没有遇到任何问题。 - Binev
@IvanBinev 我的回答前30分钟 :) - Yura Buyaroff
超级神奇,但它却能工作。为什么 FB 要搞这种疯狂的东西?你需要一个被接受的答案,这个答案和最新的 FB SDK 版本才不会出问题! - Stoycho Andreev

7
问题在于id被转换为整数: https://code.google.com/p/android/issues/detail?id=78839 在我的情况下,facebook_app_id是从build.gradle文件中的每个flavor中设置的。
解决方法是用引号将id包装起来:
flavor.resValue "string", "facebook_app_id", "\"1111111111111\""

或者,如果您想避免转义:

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

flavor.resValue "string", "facebook_app_id", '"1111111111111"' 这个解决方案对我有用。谢谢。 - Orcun Sevsay

0

这个小代码修改在活动中帮了我。

@Override
    protected void onCreate(Bundle savedInstanceState) {

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

        super.onCreate(savedInstanceState);
        ...................
        ...................    
}

-1

实际上在Gradle中你不必使用flavor codes...

如果你有超过4字节长度的数字,你应该将这段代码放在strings.xml文件中。

注意:需要注意引号(")的使用。

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

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