我使用此代码分享我的内容。我不能将此用于我的活动,但我可以将此用于我的片段!当我在我的活动中调用它时,应用程序崩溃了!请帮帮我,谢谢。
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = itemsArrayList.get(position).getDes();
//sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
context.startActivity(Intent.createChooser(sharingIntent, "اشتراک با"));
我的logcat错误:
11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:FATAL EXCEPTION:main 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:android.util.AndroidRuntimeException:从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗? 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at android.app.ContextImpl.startActivity(ContextImpl.java:1244) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at android.app.ContextImpl.startActivity(ContextImpl.java:1231) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at com.katibehpayam.mahdi.katibehpayam.adapter_common $ 4.onClick(adapter_common.java:138) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at android.view.View.performClick(View.java:4377) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at android.view.View $ PerformClick.run(View.java:18044) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at android.os.Handler.handleCallback(Handler.java:725) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:92) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at android.os.Looper.loop(Looper.java:137) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at android.app.ActivityThread.main(ActivityThread.java:5306) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at java.lang.reflect.Method.invokeNative(Native Method) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at java.lang.reflect.Method.invoke(Method.java:511) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 11-10 01:57:32.597 11723-11723 / com.katibehpayam.mahdi.katibehpayam E / AndroidRuntime:at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
如果您没有使用活动作为上下文,则必须包含标志FLAG_ACTIVITY_NEW_TASK,如错误所示。由于您是从片段中执行此操作,因此必须添加:
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);