Android facebook applicationId不能为null

Kak*_*kia 70 android facebook

我一直在按照以下教程将我的应用程序与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其中将无效.

你必须这样定义你applicationIdAndroidManifest.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)

  • 显然你必须将它放在strings.xml中,因为如果你直接在AndroidManifest.xml中引用它,它将无法工作.从那种方式出现的奇怪行为与Google的API一起使用...... (46认同)
  • 您可以将它放在AndroidManifest.xml中,并通过为字符串添加前缀来避免转换为Integer:```<meta-data android:name ="com.facebook.sdk.ApplicationId"android:value ="\ XXXXXXXXXXXXXXXXX "/>```(注意转义字符和ID字符串之间的空格) (15认同)
  • @MikeDrakoulelis说得对,因为如果你直接在AndroidManifest.xml中引用它,它会将它解析为`Integer`. (5认同)

Yur*_*off 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 有没有搞错.这只是让我的应用程序崩溃.是我还是Facebook努力窥探我们?首先,他们禁止从图形api中获取消息.然后他们在移动浏览器上禁用消息,除非您安装了需要9271491724权限的Messenger应用程序,现在他们强迫您使用他们的事件记录器记录您的应用程序..我是偏执狂吗? (2认同)

mac*_*lir 7

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

在我的情况下,每个风味facebook_app_idbuild.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)