Android:java.lang.StringIndexOutOfBoundsException:在长按事件上复制到剪贴板选定文本

时间:2015-03-23 07:40:46

标签: android

在我的应用程序中,我想在长按事件中将副本复制到剪贴板选定的文本功能.foo是文本视图 foo =(TextView)findViewById(R.id.single_string);. 我使用以下代码来实现此功能。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    activateToolbar();
    text = parseSourceCode(text);

    foo = (TextView) findViewById(R.id.single_string);
    foo.setTextSize(mRatio + 14);
    foo.setText(Html.fromHtml(text,imgGetter, null));
    foo.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            String stringYouExtracted = foo.getText().toString();
            int startIndex = foo.getSelectionStart();
            int endIndex = foo.getSelectionEnd();
            stringYouExtracted = stringYouExtracted.substring(startIndex, endIndex);
            if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
                android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                clipboard.setText(stringYouExtracted);
            } else {
                android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", stringYouExtracted);
                clipboard.setPrimaryClip(clip);
            }
            // TODO Auto-generated method stub
            return false;
        }
    });

我可以设置文本,但是当我在屏幕上长按以复制所选文本时,我的代码会崩溃。 我得到的错误是:

java.lang.StringIndexOutOfBoundsException: length=3704; regionStart=-1; regionLength=0 at 
java.lang.String.startEndAndLength(String.java:504) at
java.lang.String.substring(String.java:1333) at 
java.lang.String.subSequence(String.java:1671) – 

2 个答案:

答案 0 :(得分:1)

你错过了()

String stringYouExtracted = foo.getText().toString(); -- add ()

检查实例toString() from String - subString实际上是substring

stringYouExtracted = stringYouExtracted.substring(startIndex, endIndex);

p.s。: (根据评论):

stringYouExtracted = stringYouExtracted.substring(startIndex, endIndex);
ClipboardManager clipboard = (ClipboardManager)
                              getSystemService(CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("text", stringYouExtracted);
clipboard.setPrimaryClip(clipData);

检查http://developer.android.com/reference/android/content/ClipData.html处的示例。

p.p.s。: 使用android:textIsSelectableTextView可选

制作文字
<TextView
    android:id="@+id/single_string"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textIsSelectable="true"
    android:text="Some text" />

答案 1 :(得分:0)

  foo.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            String stringYouExtracted = foo.getText().toString();
            int startIndex = foo.getSelectionStart();
            int endIndex = foo.getSelectionEnd();
            stringYouExtracted = stringYouExtracted.subString(startIndex, endIndex);
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            clipboard.setText(stringYouExtracted);

            // TODO Auto-generated method stub
            return false;
        }
    });

toString()不是toString,它是一种方法。

EDIT1: subString方法还有一个错误,正确的方法是substring(startIndex, endIndex);