-2

I'm trying to copy some text from an editText field in android studio but the app always crashes when I do. copying works fine, what crashes the app is copying from editText.

I assumed it is String and tried casting it to a CharSequence, still crashes. I also tried copying from a TextView with no success, could it be the casting?

button3.setOnClickListener(
                new Button.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        EditText editText = findViewById(R.id.editText);
                        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                        ClipData clip = ClipData.newPlainText("aaa",(CharSequence)editText);
                        clipboard.setPrimaryClip(clip);
                    }
                }
        );

stack trace:

2019-04-28 03:46:47.121 29744-29744/my.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: my.app, PID: 29744
    java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to java.lang.CharSequence
        at my.app.MainActivity$3.onClick(MainActivity.java:68)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

btw MainActivity.java:68 is ClipData clip = ClipData.newPlainText("aaa",(CharSequence)editText);

kuha
  • 1
  • 1
  • String text = editText.getText(); Set the above line on button click listener. – Prafulla Nayak Apr 28 '19 at 00:45
  • `(CharSequence)editText` – That's not how to get the text from an `EditText`. You need to call `getText()` on the `EditText` (as Prafulla Nayak mentions), and optionally `toString()` on that, if you want plaintext. If it's also crashing when you do that correctly, we'll need to see that stack trace, too. – Mike M. Apr 28 '19 at 00:52

1 Answers1

-1

you don't have to use ClipboardManager or ClipData to copy from your EditText or TextView all you have to do is add this attribute to your TextView or EditText to do the work

android:textIsSelectable="true"

edit

button3.setOnClickListener(
                new Button.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        EditText editText = findViewById(R.id.editText);
                        String text = editText.getText();
                        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                        ClipData clip = ClipData.newPlainText("aaa",text);
                        clipboard.setPrimaryClip(clip);
                    }
                }
        );
ismail alaoui
  • 5,748
  • 2
  • 21
  • 38