applicationId不能为null,但xml语法是正确的

时间:2014-01-05 04:04:01

标签: java android xml facebook libgdx

我试图将Facebook添加到已经完成的LibGDX游戏中。但是,当我尝试检查当前会话时,它返回null。我试图改为使用新的Session,我得到一个空指针错误:

01-04 19:49:15.433: E/AndroidRuntime(16540): FATAL EXCEPTION: main
01-04 19:49:15.433: E/AndroidRuntime(16540): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pressx.thedevice/com.pressx.thedevice.MainActivity}: java.lang.NullPointerException: Argument 'applicationId' cannot be null
01-04 19:49:15.433: E/AndroidRuntime(16540):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at android.app.ActivityThread.access$600(ActivityThread.java:135)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at android.os.Looper.loop(Looper.java:137)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at android.app.ActivityThread.main(ActivityThread.java:4849)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at java.lang.reflect.Method.invokeNative(Native Method)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at java.lang.reflect.Method.invoke(Method.java:511)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at dalvik.system.NativeStart.main(Native Method)
01-04 19:49:15.433: E/AndroidRuntime(16540): Caused by: java.lang.NullPointerException: Argument 'applicationId' cannot be null
01-04 19:49:15.433: E/AndroidRuntime(16540):    at com.facebook.internal.Validate.notNull(Validate.java:29)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at com.facebook.Session.<init>(Session.java:227)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at com.facebook.Session.<init>(Session.java:216)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at com.facebook.Session$Builder.build(Session.java:1576)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at com.facebook.Session.openActiveSession(Session.java:888)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at com.facebook.Session.openActiveSession(Session.java:830)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at com.pressx.facebook.FacebookAndroid.<init>(FacebookAndroid.java:27)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at com.pressx.thedevice.MainActivity.onCreate(MainActivity.java:17)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at android.app.Activity.performCreate(Activity.java:5244)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
01-04 19:49:15.433: E/AndroidRuntime(16540):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
01-04 19:49:15.433: E/AndroidRuntime(16540):    ... 11 more

如果Facebook从AndroidManifest.xml检查ApplicationID值,那么正确的语法是什么?我把它作为我的元数据线:

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

将此作为我的值字符串:

<string name="app_id">###############</string>

目前我的AndroidManifest.xml已满,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.pressx.thedevice" android:versionCode="1" android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
        <meta-data android:name="ApplicationId" android:value="@string/app_id"/>
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id" />
        <activity android:name="com.facebook.LoginActivity">
        </activity>
        <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="landscape" android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
           </activity>
    </application>
</manifest>

我的完整values.xml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">The Device</string>
    <string name="app_id">###############</string>
</resources>

任何人都有解决此问题的方法吗?您是否需要更多信息,例如我的项目设置或类似内容?

1 个答案:

答案 0 :(得分:3)

<meta-data 
    android:name="com.facebook.sdk.ApplicationId" 
    android:value="@string/app_id" />
必须在<application />范围内声明

,而不是<activity />范围。将标记移动到活动外部,如下所示:

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<!-- Note new location -->
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id" />
<meta-data android:name="ApplicationId" android:value="@string/app_id"/>
    <activity android:name="com.facebook.LoginActivity"></activity>
    <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="landscape" android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>