简体   繁体   中英

Android: problem in getText method of EditText

In my project I have two activities or classes. In first activity I have a EditText and I want to get the text of it from second class.

In the first class I wrote this code but it seems has problem.

public String getTextMessage()
{
    return textMessage.getText().toString();
}

because in second class when I want to get it, program crashes.

message = encode.getTextMessage();

What is your suggestion?

As told by sunil you have to firstly get string from edittextbox and through intent send it to another second activity. After start of second activity you have to get text from bundle. code snippet is given below...

Activity A

            Intent i = new Intent(this, Second.class);
            i.putExtra("EXTRATEXT", editText.gettext().toString());
            startActivity(i);

Activity B

Class Second extends Activity{
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String text = getIntent().getExtras().getString("EXTRATEXT");
    }

Access the text by getText() from edit text and store it in an string. when you move to second activity sent string variable to second class via bundel. Extract the bundel in second class and use 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