开发者

Android: problem in getText method of EditText

开发者 https://www.devze.com 2023-01-27 18:44 出处:网络
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 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.


You have to pass the value through intents

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号