Passing string from edit text to another activity

白昼怎懂夜的黑 提交于 2019-12-09 07:18:06

问题


I have looked through the example here on stack overflow. However, I can't get a solution that works correctly. My application still crashes. How do I pass the string from an edit text in one activity to another activity?

This is my code from the first activity:

Button btnGo = (Button) findViewById(R.id.btnGo);

btnGo.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        EditText etLocation = (EditText) findViewById(R.id.et_location);
        Intent intent = new Intent();
        intent.putExtra("location", etLocation.getText().toString());
        startActivity(intent);
    }
}

Code from Second Activity:

textView1 = (TextView) findViewById(R.id.textView1);

Intent intent = getIntent();
String str = intent.getStringExtra("location");
textView1.setText(str);

回答1:


Change:

Intent intent = new Intent();

to:

Intent intent = new Intent(MyCurrentActivityClass.this, NextActivity.class);

Make sure NextActivity is in the Manifest. In the first case you're not providing enough info to start the activity.




回答2:


Try this:

From first activity send like this:

btnGo.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        EditText etLocation = (EditText) findViewById(R.id.et_location);
       Intent i = new Intent(this, ActivityTwo.class);
        i.putExtra("location", etLocation.getText().toString());      
        startActivity(i);
}
});

And in second activity do like this:

Intent in = getIntent();
String tv1= in.getExtras().getString("location");
textView1.setText(tv1);



回答3:


You should get your information from the second activity this way:

Bundle extras = getIntent().getExtras();
String myLocation= extras.getString("location");



回答4:


Did you declare a variable textview1? Change

textView1 = (TextView) findViewById(R.id.textView1); to

TextView textView1 = (TextView) findViewById(R.id.textView1);



来源:https://stackoverflow.com/questions/18481516/passing-string-from-edit-text-to-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!