Android facebook applicationId不能为null

时间:2013-04-22 21:15:30

标签: android facebook

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

我已经按照教程中的所有内容进行了操作,但在两种情况下我得到了applicationId cannot be null,这真的令人沮丧。

我的FacebookActivity onCreate有以下内容,与教程完全相同:

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 个答案:

答案 0 :(得分:224)

  

TL; DR:您 在您的strings.xml中编写应用程序的ID,然后引用(即@strings/fb_app_id),因为如果您直接(作为值)进入AndroidManifest.xml它将无效。

您必须在applicationId中定义AndroidManifest.xml 像这样:

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

<{1>}

下的

其中<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"/>标记

之内

- 并在strings.xml中

<application>

答案 1 :(得分:8)

今天答案不太正确。 如果有人不使用此: AppEventsLogger.activateApp(this);

自上次更新以来,您必须这样做,否则您的应用会崩溃。 您还应该在此处传递应用,而不是上下文

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 :(得分:7)

问题是id被转换为整数: https://code.google.com/p/android/issues/detail?id=78839

在我的情况下,facebook_app_id是根据每种风格的build.gradle文件设置的。

解决方案是使用"包装id:

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

或者你宁愿避免逃避:

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

答案 3 :(得分:0)

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

@Override
    protected void onCreate(Bundle savedInstanceState) {

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

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

答案 4 :(得分:-1)

实际上你不必在gradle中使用味道代码...

如果您的数字超过4个字节,则应在strings.xml中使用此代码

  

注意:注意此引号(

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