我有一个奇怪的问题。我需要根据选择的社交媒体分享不同的内容。
我发现了一些可以让我这样做的东西,当我需要实现与 Facebook 的共享时,它就成了一个问题(Facebook 忽略了意图)
这是我正在使用的方法(不在片段中,因此通过片段)
它工作得很好(例如,发送到推特“abc”并发送到短信“def”)。
在我实现 facebook 的那一刻,它与下面的堆栈跟踪崩溃了
public static void onShareClick(Fragment context, Trade trade) {
Resources resources = context.getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
// Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native).replace(resources.getString(R.string.share_trade_url_placeholder), StringUtils.decodeUrl(StringUtils.createItemURIWithTitle(trade.getTradeId(), trade.getTitle())))));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
emailIntent.setType("message/rfc822");
PackageManager pm = context.getActivity().getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if (packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if (packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if (packageName.contains("twitter")) {
//aContext.getString(R.string.social_tweet_look_at_this_on_myapp_text) + StringUtils.decodeUrl(StringUtils.createItemURIWithTitle(aTradeId, tradeTitle)) + aContext.getString(R.string.social_tweet_hashtag_myapp_text)
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter).replace(resources.getString(R.string.share_trade_url_placeholder), StringUtils.decodeUrl(StringUtils.createItemURIWithTitle(trade.getTradeId(), trade.getTitle()))));
} else if (packageName.contains("facebook")) {
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentTitle("This is the title")
.setContentDescription("This is the description")
.setContentUrl(Uri.parse("www.google.com"))
.build();
ShareDialog shareDialog = new ShareDialog(context);
//---------This is where the error is----------//
shareDialog.show(content, ShareDialog.Mode.AUTOMATIC);
}
// intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook).replace(resources.getString(R.string.share_trade_url_placeholder), StringUtils.decodeUrl(StringUtils.createItemURIWithTitle(trade.getTradeId(), trade.getTitle()))));
// Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
// One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
// will show the <meta content ="..."> text from that page with our link in Facebook.
} else if (packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms).replace(resources.getString(R.string.share_trade_url_placeholder), StringUtils.decodeUrl(StringUtils.createItemURIWithTitle(trade.getTradeId(), trade.getTitle()))));
} else if (packageName.contains("android.gm")) { // If Gmail shows up twice, try removing this else-if clause and the reference to "android.gm" above
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail).replace(resources.getString(R.string.share_trade_url_placeholder), StringUtils.decodeUrl(StringUtils.createItemURIWithTitle(trade.getTradeId(), trade.getTitle())))));
intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
intent.setType("message/rfc822");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
context.startActivity(openInChooser);
}
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myApp.app, PID: 532 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746) at com.facebook.internal.Utility.getDialogFeatureConfig(Utility.java:922) at com.facebook.internal.DialogPresenter.getVersionSpecForFeature(DialogPresenter.java:254) at com.facebook.internal.DialogPresenter.getProtocolVersionForNativeDialog(DialogPresenter.java:241) at com.facebook.internal.DialogPresenter.canPresentNativeDialogWithFeature(DialogPresenter.java:75) at com.facebook.share.widget.ShareDialog.canShowNative(ShareDialog.java:162) at com.facebook.share.widget.ShareDialog.access$300(ShareDialog.java:65) at com.facebook.share.widget.ShareDialog$NativeHandler.canShow(ShareDialog.java:314) at com.facebook.share.widget.ShareDialog$NativeHandler.canShow(ShareDialog.java:285) at com.facebook.internal.FacebookDialogBase.createAppCallForMode(FacebookDialogBase.java:210) at com.facebook.internal.FacebookDialogBase.showImpl(FacebookDialogBase.java:150) at com.facebook.share.widget.ShareDialog.show(ShareDialog.java:267) at com.myApp.app.item.overview.OverviewListItemFragment.onShareClick(OverviewListItemFragment.java:908) at com.myApp.app.item.overview.OverviewListItemFragment.onClick(OverviewListItemFragment.java:841) at com.myApp.app.item.overview.OverviewListItemFragment_ViewBinding$10.doClick(OverviewListItemFragment_ViewBinding.java:169) at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22) at android.view.View.performClick(View.java:4802) at android.view.View$PerformClick.run(View.java:20101) at android.os.Handler.handleCallback(Handler.java:810) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:189) at android.app.ActivityThread.main(ActivityThread.java:5529) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)