Textview可选共享选项崩溃

时间:2017-08-24 18:29:14

标签: android android-activity textview textselection

我想在textview中与文本选择工具栏共享所选文本。 这是我的textview.xml

<TextView
                android:id="@+id/txt_content"
                fontPath="fonts/Roboto-Regular.ttf"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:text="@{data}"
                android:bufferType="spannable"
                android:textColor="@color/md_black_1000"
                android:textIsSelectable="true"
                android:textSize="22sp" />

textview内容是一个带有html代码

的spannable
binding.setData(SearchTextHighlight.highlight(searchKeyWord, Html.fromHtml(articles.getContent())));

当我选择共享时出现以下错误。我希望我的问题很明确。

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
                                                                       at android.app.ContextImpl.startActivity(ContextImpl.java:740)
                                                                       at android.app.ContextImpl.startActivity(ContextImpl.java:727)
                                                                       at android.content.ContextWrapper.startActivity(ContextWrapper.java:345)
                                                                       at android.widget.TextView.shareSelectedText(TextView.java:11284)
                                                                       at android.widget.TextView.onTextContextMenuItem(TextView.java:10833)
                                                                       at android.widget.Editor$TextActionModeCallback.onActionItemClicked(Editor.java:3681)
                                                                       at com.android.internal.policy.PhoneWindow$DecorView$ActionModeCallback2Wrapper.onActionItemClicked(PhoneWindow.java:4170)
                                                                       at com.android.internal.view.FloatingActionMode$4.onMenuItemSelected(FloatingActionMode.java:108)
                                                                       at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:801)
                                                                       at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:155)
                                                                       at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:954)
                                                                       at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:944)
                                                                       at com.android.internal.view.FloatingActionMode$5.onMenuItemClick(FloatingActionMode.java:136)
                                                                       at com.android.internal.widget.FloatingToolbar$FloatingToolbarMainPanel$1.onClick(FloatingToolbar.java:1298)
                                                                       at android.view.View.performClick(View.java:5698)
                                                                       at android.widget.TextView.performClick(TextView.java:10888)
                                                                       at android.view.View$PerformClick.run(View.java:22570)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:158)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:7231)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

1 个答案:

答案 0 :(得分:0)

将此行添加到您用于分享的意图

Intent i = new Intent(.........);
//add this line
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);