弃用的 FacebookSdk 方法抛出 RuntimeException

Deprecated FacebookSdk method throws RuntimeException

我有 FacebookSdk.sdkInitialize(getApplicationContext()),其中 sdkInitialize() 显示为已弃用。根据此 我们可以删除该行。但是后来 AppEventsLogger.activateApp(this) 之后的行出现以下错误:

AndroidRuntime: FATAL EXCEPTION: main                                                                              Process: com.daimler.moovel.android:auth, PID: 4011                                java.lang.RuntimeException: Unable to create application com.daimler.moovel.android.DebugApplication: The Facebook sdk must be initialized before calling activateApp                                              at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5879)                                             at android.app.ActivityThread.-wrap3(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1699)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: The Facebook sdk must be initialized before calling activateApp
at com.facebook.appevents.AppEventsLogger.activateApp(AppEventsLogger.java:226)
at com.facebook.appevents.AppEventsLogger.activateApp(AppEventsLogger.java:208)

所以我错过了什么?

那是因为您升级了 Facebook SDK 并且您正在尝试使用 AppEventsLogger providinig this 的实现作为 Context:

AppEventsLogger.activateApp(this);

从 SDK 4.19 及更高版本替换为:

AppEventsLogger.activateApp(getApplication());

关于这个的文档说:

Notifies the events system that the app has launched and activate and deactivate events should start being logged automatically. This should be called from the OnCreate method of you application.

如果 Facebook SDK 现在在应用程序启动时自动初始化,那有逻辑。

试试看,希望能解决你的问题。

不需要 AppEventsLogger.activateApp(this); 现在如果你在 manifest.xml 中设置了 facebook_id 就不需要了 您只需在 manifest.xml

中的应用程序标签中添加以下内容
 <meta-data
   android:name="com.facebook.sdk.ApplicationId"          
   android:value="@string/facebook_app_id" /> 

其中 facebook_app_id 在 string.xml

中定义