user1048684
user1048684

Reputation: 19

Pass value obtained from EditText box to a TextView in the next screen

I have two java classes, HelloAndroidActivity and GetTasks. I want to try to get the text from the Edit Text box from the first activity on clicking the button and get that value in the next activity GetTasks and display it in the text view. My code is as shown:

HelloAndroidActivity

Button save = (Button) findViewById(R.id.save);
    save.setOnClickListener(new OnClickListener() {

        public void onClick (View v) {

            Intent i = new Intent(HelloAndroidActivity.this, GetTasks.class);
            //i.setClass(HelloAndroidActivity.this, GetTasks.class);
            EditText taskname = (EditText) findViewById(R.id.task_name);
            String task_name = taskname.getEditableText().toString();
            Log.d("Task Name", task_name + "");
            i.putExtra("taskname", task_name);
            startActivity(i);

        }
    });

GetTasks

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_page_layout);

    CharSequence task_name = (CharSequence) findViewById(R.id.task_name);
    Log.d("Here", task_name + "");

    Intent i2 = getIntent();
    taskname = i2.getStringExtra("taskname");

    TextView text = (TextView) findViewById(R.id.gettaskname);
    text.setText(taskname);


}    

Can you tell me what am I doing wrong? My application force closes itself. Instead of passing a variable, if I pass a string variable, I am able to see that in the text view? Does it have to do with the manifest file? I have an intent for both the activities. Any help regarding this would appreciated.

Upvotes: 1

Views: 1215

Answers (2)

Bryan
Bryan

Reputation: 3667

Instead of (CharSequence), use (EditText), that is the type of layout object your are retrieving the data from, as defined your layout XML file.


In your GetTasks.onCreate method, you need to bring in the values that you passed from the intent in the HelloAndroidActivity.

You do that like this:

Bundle extras = getIntent().getExtras();
if (extras ==null) { return;} 
String taskname = extras.getString("taskname");

See the following link for a good tutorial on using intents

Upvotes: 0

mastDrinkNimbuPani
mastDrinkNimbuPani

Reputation: 1249

I believe that the line

CharSequence task_name = (CharSequence) findViewById(R.id.task_name);

is the most likely culprit. CharSequence should replaced with whatever type of a view the task_name element is...

Upvotes: 2

Related Questions