我想使用其SDK分享Facebook上的內容。我的參考是基於Facebook SDK 4.0.0的Facebook上的Share Dialog。Facebook SDK 4.0.0,Android上的ShareDialog始終爲空
CallbackManager callbackManager;
ShareDialog shareDialog;
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().addFlags(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.v3_activity_splash);
// Init Facebook SDK
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
shareOnFacebook("This is test...");
...
}
private void shareOnFacebook(String content)
{
if (ShareDialog.canShow(ShareLinkContent.class))
{
Log.e("Test", "inside shareOnFacebook()");
// ShareLinkContent shareLinkContent = new ShareLinkContent.Builder().setContentDescription(content).build();
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Hello Facebook")
.setContentDescription(content)
.setContentUrl(Uri.parse("http://developers.facebook.com/android"))
.build();
shareDialog.show(linkContent);
}
else
{
Toast.makeText(this, "NOT CALLED", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
Log.e("Test", "Yes, Uuu bitch!!!");
}
我也有代碼以下行中我manifest文件Application
標籤內:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="MY_APP_ID"/>
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProviderMY_APP_ID"
android:exported="true"/>
我的問題是NullPointerException異常,當我發動shareDialog.show(linkContent);
線應用程序/活動。然而,有趣的是shareDialog
IS NOT NULL根據我調試:(所以不知道爲什麼「NullPointerException異常」拋出。我還沒有看到我日誌的onActivityResult
因此該功能不會被調用。
最後日誌顯示:
Process: MY_PCKAGE_NAME, PID: 2152
java.lang.RuntimeException: Unable to start activity ComponentInfo{MY_PCKAGE_NAME/MY_PCKAGE_NAME.v3.splash.SplashActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2281)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5230)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:780)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:596)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:883)
at com.facebook.internal.Utility.getDialogFeatureConfig(Utility.java:859)
at com.facebook.internal.DialogPresenter.getVersionSpecForFeature(DialogPresenter.java:247)
at com.facebook.internal.DialogPresenter.getProtocolVersionForNativeDialog(DialogPresenter.java:234)
at com.facebook.internal.DialogPresenter.canPresentNativeDialogWithFeature(DialogPresenter.java:75)
at com.facebook.share.widget.ShareDialog.canShowNative(ShareDialog.java:133)
at com.facebook.share.widget.ShareDialog.access$300(ShareDialog.java:55)
at com.facebook.share.widget.ShareDialog$NativeHandler.canShow(ShareDialog.java:241)
at com.facebook.share.widget.ShareDialog$NativeHandler.canShow(ShareDialog.java:233)
at com.facebook.internal.FacebookDialogBase.createAppCallForMode(FacebookDialogBase.java:184)
at com.facebook.internal.FacebookDialogBase.showImpl(FacebookDialogBase.java:147)
at com.facebook.internal.FacebookDialogBase.show(FacebookDialogBase.java:142)
at MY_PCKAGE_NAME.splash.SplashActivity.shareOnFacebook(SplashActivity.java:414)
at MY_PCKAGE_NAME.splash.SplashActivity.onCreate(SplashActivity.java:93)
at android.app.Activity.performCreate(Activity.java:5411)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5230)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:780)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:596)
at dalvik.system.NativeStart.main(Native Method)
任何建議,將不勝感激感謝
我在manifeset中添加了FacebookActivity,元數據和提供程序,並且還使用了字符串資源來保存FB_app_id值,我只在元標記中使用了這個值,在提供者標記中使用了實際的id,但仍然有相同的錯誤。你能提出一些建議嗎? – Crawler 2015-06-12 11:03:54
@crwler如果以上兩項是正確的,請確保您生成的哈希碼也可以。我的建議是使用他們的示例代碼來生成一個,否則,如果您基於命令生成,那麼它總是創建一個,而不管命令是正確還是錯誤。 – Hesam 2015-06-13 05:04:04
我有這個問題,並從您的評論,然後我看了一下更多的源,並在Facebook SDK初始化有一個有趣的位。 您必須鏈接到字符串資源,或用'fb'作爲ID前綴。所以: 基本上,代碼需要屬性值爲String。如果你只是輸入你的ID,那麼它顯然是以int的形式輸入的。如果他們發現這個值,他們實際上會拋出一個異常,但它是一個int而不是僅僅處理它。我猜想如果他們以後需要alpha代碼的話,這是未來的保證。 –
Simon
2016-04-06 15:05:49