我一直在遵循下面的教程,将我的应用程序与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,但是什么都没有改变。
到底是什么导致了这一切?
发布于 2013-04-23 05:19:13
TL;DR:您让将应用程序的ID写入您的
strings.xml中,然后引用(即@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>发布于 2016-06-17 06:48:31
从今天开始答案就不太正确了。如果有人没有使用这个:AppEventsLogger.activateApp(this);
自上次更新以来,您必须这样做,否则您的应用程序将崩溃。您还应该在此处传递应用程序,而不是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);
}
}发布于 2015-04-09 00:01:07
问题是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"'https://stackoverflow.com/questions/16156856
复制相似问题