我一直在按照以下教程将我的应用程序与Facebook集成. Facebook教程
我已经按照教程中的所有内容进行了操作,但是我遇到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();
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试显示我得到的活动时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"/>
Run Code Online (Sandbox Code Playgroud)
我尝试使用代码获取Id,但没有任何改变.
究竟是什么造成了这一切?
Moh*_*san 224
TL; DR:您必须在您的应用程序中编写应用程序ID
strings.xml,然后引用(即@strings/fb_app_id),因为如果将其直接(作为值)放入AndroidManifest.xml其中将无效.
你必须这样定义你applicationId的AndroidManifest.xml
:
<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>
Run Code Online (Sandbox Code Playgroud)
**请注意标签<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>内<application>
- 并在strings.xml中
<string name="app_id">1389xxxxxxxx</string>
Run Code Online (Sandbox Code Playgroud)
从今天开始,答案并不完全正确.如果有人不使用此:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是id被转换为整数:https://code.google.com/p/android/issues/detail? id = 78839
在我的情况下,每个风味facebook_app_id的build.gradle文件设置.
解决方案是用以下内容包装id ":
flavor.resValue "string", "facebook_app_id", "\"1111111111111\""
Run Code Online (Sandbox Code Playgroud)
或者如果你想避免逃避:
flavor.resValue "string", "facebook_app_id", '"1111111111111"'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39104 次 |
| 最近记录: |