我跟随https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/获取android sdk,所有内容都很好,因为它在给定的喜欢上描述,但是当我运行我的项目时,我得到错误“不幸的是'appname'已停止工作”这里是我的logcat错误:
09-26 17:37:06.662: E/AndroidRuntime(5849): FATAL EXCEPTION: main
09-26 17:37:06.662: E/AndroidRuntime(5849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.facebook.hellofriend/com.facebook.hellofriend.MainActivity}: java.lang.NullPointerException: Argument 'applicationId' cannot be null
09-26 17:37:06.662: E/AndroidRuntime(5849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
09-26 17:37:06.662: E/AndroidRuntime(5849): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)
09-26 17:37:06.662: E/AndroidRuntime(5849): at android.app.ActivityThread.access$600(ActivityThread.java:139)
09-26 17:37:06.662: E/AndroidRuntime(5849): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
09-26 17:37:06.662: E/AndroidRuntime(5849): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 17:37:06.662: E/AndroidRuntime(5849): at android.os.Looper.loop(Looper.java:154)
09-26 17:37:06.662: E/AndroidRuntime(5849): at android.app.ActivityThread.main(ActivityThread.java:4974)
09-26 17:37:06.662: E/AndroidRuntime(5849): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 17:37:06.662: E/AndroidRuntime(5849): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 17:37:06.662: E/AndroidRuntime(5849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-26 17:37:06.662: E/AndroidRuntime(5849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-26 17:37:06.662: E/AndroidRuntime(5849): at dalvik.system.NativeStart.main(Native Method)
09-26 17:37:06.662: E/AndroidRuntime(5849): Caused by: java.lang.NullPointerException: Argument 'applicationId' cannot be null
09-26 17:37:06.662: E/AndroidRuntime(5849): at com.facebook.internal.Validate.notNull(Validate.java:29)
09-26 17:37:06.662: E/AndroidRuntime(5849): at com.facebook.Session.<init>(Session.java:227)
09-26 17:37:06.662: E/AndroidRuntime(5849): at com.facebook.Session.<init>(Session.java:216)
09-26 17:37:06.662: E/AndroidRuntime(5849): at com.facebook.Session$Builder.build(Session.java:1575)
09-26 17:37:06.662: E/AndroidRuntime(5849): at com.facebook.Session.openActiveSession(Session.java:888)
09-26 17:37:06.662: E/AndroidRuntime(5849): at com.facebook.Session.openActiveSession(Session.java:830)
09-26 17:37:06.662: E/AndroidRuntime(5849): at com.facebook.hellofriend.MainActivity.onCreate(MainActivity.java:19)
09-26 17:37:06.662: E/AndroidRuntime(5849): at android.app.Activity.performCreate(Activity.java:4538)
09-26 17:37:06.662: E/AndroidRuntime(5849): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
09-26 17:37:06.662: E/AndroidRuntime(5849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2158)
09-26 17:37:06.662: E/AndroidRuntime(5849): ... 11 more
感谢。
我的清单文件是:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.facebook.hellofriend.MainActivity"
android:label="@string/app_name" >
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="21907****928145"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的新logcat错误:( 09-26 18:55:54.564:E / AndroidRuntime(11123):致命异常:主要 09-26 18:55:54.564:E / AndroidRuntime(11123):java.lang.RuntimeException:无法启动活动ComponentInfo {com.facebook.hellofriend / com.facebook.hellofriend.MainActivity}:java.lang.NullPointerException:Argument 'applicationId'不能为空 09-26 18:55:54.564:E / AndroidRuntime(11123):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202) 09-26 18:55:54.564:E / AndroidRuntime(11123):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237) 09-26 18:55:54.564:E / AndroidRuntime(11123):在android.app.ActivityThread.access $ 600(ActivityThread.java:139) 09-26 18:55:54.564:E / AndroidRuntime(11123):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1262) 09-26 18:55:54.564:E / AndroidRuntime(11123):在android.os.Handler.dispatchMessage(Handler.java:99) 09-26 18:55:54.564:E / AndroidRuntime(11123):在android.os.Looper.loop(Looper.java:154) 09-26 18:55:54.564:E / AndroidRuntime(11123):在android.app.ActivityThread.main(ActivityThread.java:4974) 09-26 18:55:54.564:E / AndroidRuntime(11123):at java.lang.reflect.Method.invokeNative(Native Method) 09-26 18:55:54.564:E / AndroidRuntime(11123):at java.lang.reflect.Method.invoke(Method.java:511) 09-26 18:55:54.564:E / AndroidRuntime(11123):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 09-26 18:55:54.564:E / AndroidRuntime(11123):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 09-26 18:55:54.564:E / AndroidRuntime(11123):at dalvik.system.NativeStart.main(Native Method) 09-26 18:55:54.564:E / AndroidRuntime(11123):引起:java.lang.NullPointerException:参数'applicationId'不能为null 09-26 18:55:54.564:E / AndroidRuntime(11123):at com.facebook.internal.Validate.notNull(Validate.java:29) 09-26 18:55:54.564:E / AndroidRuntime(11123):at com.facebook.Session。(Session.java:227) 09-26 18:55:54.564:E / AndroidRuntime(11123):at com.facebook.Session。(Session.java:216) 09-26 18:55:54.564:E / AndroidRuntime(11123):at com.facebook.Session $ Builder.build(Session.java:1575) 09-26 18:55:54.564:E / AndroidRuntime(11123):at com.facebook.Session.openActiveSession(Session.java:888) 09-26 18:55:54.564:E / AndroidRuntime(11123):at com.facebook.Session.openActiveSession(Session.java:830) 09-26 18:55:54.564:E / AndroidRuntime(11123):at com.facebook.hellofriend.MainActivity.onCreate(MainActivity.java:19) 09-26 18:55:54.564:E / AndroidRuntime(11123):在android.app.Activity.performCreate(Activity.java:4538) 09-26 18:55:54.564:E / AndroidRuntime(11123):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) 09-26 18:55:54.564:E / AndroidRuntime(11123):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2158) 09-26 18:55:54.564:E / AndroidRuntime(11123):... 11更多
答案 0 :(得分:1)
您必须在清单文件中添加和应用ID
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/fb_app_id"/>
以上</application>
标记。
"@string/fb_app_id"
是您在strings.xml
文件中的Facebook开发者控制台中的Fb应用ID。
修改强>
如果您确定您的清单如下所示:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.facebook.hellofriend.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="21907****928145"/>
</application>
尝试在代码中设置应用ID:
session = new Session(this);
session = new Session.Builder(this).setApplicationId("21907****928145").build();
编辑:(在代码中使用set app id的示例)
创建新类并将其放在主包中:
public class MyApp extends Application{
@Override
public void onCreate() {
super.onCreate();
session = new Session(this);
session = new Session.Builder(this).setApplicationId("21907****928145").build();
}
}
重命名<application>
in AndroidManifest.xml
,如下所示:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name=".MyApp">
...
答案 1 :(得分:1)
您指的是字符串“@ string / 21907 **** 928145”
我猜你有一个名为“21907 **** 928145”的字符串资源,并且app键为值。
String资源的名称不能以数字开头。