简体   繁体   中英

Error: Non-Static method gettext() cannot be refrenced from a static content in EditText.getText().toString();

I am trying to add a button that will copy the text and i got this error. What is static content. what should i use if I can't use this?

cButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
                    String text;
                    text=EditText.getText().toString();

                    myClip=ClipData.newPlainText("this is the text", text);
                    Toast.makeText(getApplicationContext(),"Text Sucessfully Copied",Toast.LENGTH_SHORT).show();

                }
            });

You are calling EditText.getText(). getText() is not a static function. You need to call getText() on an instance of an EditText object. Something like

(EditText)view.getText()

Same thing with ClipData.newPlainText. You've got to have an object instance before you can call that one.

Firstly, you need to bind your editText by using findViewByID() method and then;

Instead of this,

EditText.getText().toString();

Try to use this:

EditText yourEditText = (EditText) findViewById(R.id.yourEditTextId)
yourEditText.getText().toString();

Simply it's necessary to find the view by ID from your XML layout. Then, you do your work with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM