编程语言
首页 > 编程语言> > java-applicationId不能为null,但xml语法正确

java-applicationId不能为null,但xml语法正确

作者:互联网

我正在尝试将Facebook添加到已经完成的LibGDX游戏中.但是,当我尝试检查当前Session时,它返回null.我试图建立一个新的会话来代替,但出现了空指针错误:

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>

有人对此问题有解决方案吗?您是否需要诸如我的项目设置之类的更多信息?

解决方法:

<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>

标签:libgdx,facebook,xml,java,android
来源: https://codeday.me/bug/20191122/2058595.html