0

Im looking to take the value from a edittext field and pass it to another activity as a string. I have done below as shown. I think its working but I would like be to able to print the text out to see if the variable value actually passed over

But once I type Log.d("PJM",value); the activty closes with a error

MainScreen Activity

public class MainScreen extends Activity {

Button btnSearch, btnFavourites;
EditText enterDefinition;
Editable word;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);

    btnSearch=(Button)findViewById(R.id.btnSearch);
    btnFavourites=(Button)findViewById(R.id.btnFavourites);
    enterDefinition=(EditText)findViewById(R.id.enterDefinition);

    String text = enterDefinition.getText().toString();

    Intent i = new Intent(this, Definition.class);
    i.putExtra("KEY",text);
    //startActivity(i);

    //Intent i = new Intent(this, Definition.class);
    //i.putExtra("text_Label",theText);
    //startActivity(i);

Definition Activity

public class Definition extends Activity {

Button btnSend, btnSaveFave, btnReturn;
EditText enterDefinition;
String value;
TextView definition;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_definition);

    btnSend=(Button)findViewById(R.id.btnSend);
    btnSaveFave=(Button)findViewById(R.id.btnSaveFave);
    btnReturn=(Button)findViewById(R.id.btnReturn);
    enterDefinition=(EditText)findViewById(R.id.enterDefinition);
    definition=(TextView)findViewById(R.id.tvDefinition);

    definition.setText("some text");

    Bundle extras = getIntent().getExtras();
    String value = null; 
    if (extras != null) {
      value = extras.getString("KEY");
    }
    else {
      value = "None";
    }
    Log.d("text entered", value);
    Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
Karl
  • 45
  • 7

1 Answers1

1

You want:

String value = null; 
if (extras != null) {
  value = extras.getString("KEY");
}
else {
  value = "None"
}
Log.d("PJM", value);
  • Ok, well there is a problem with the Bundle then. Can you post the full code? Are you definitely calling startActivity(i, ) in the first activity? – confused_at_times Apr 17 '13 at 12:00
  • Actually, even better. Run in debug mode, put a breakpoint at the if statement in the second activity, and check the content of extras by hovering the mouse pointer over variable. – confused_at_times Apr 17 '13 at 12:01
  • Look at the code I edited in my first question, I am not starting the activity as it just means once i start my app it runs straight into the definition activity. should i put the code to pass the intent into the onClickListener for running that activity? – Karl Apr 17 '13 at 12:12
  • Yeah, that's what I was thinking. So where in the process is this Intent i used? In the code you posted, it is completely redundant. Certainly, if you have an listener for a button click that then starts the Definitions activity, then you have to put the intent in its onClick method, and call startActivity(i) from within there. – confused_at_times Apr 17 '13 at 12:15
  • ie onClick (View v) { Intent i = new Intent(this, Definition.class); i.putExtra("KEY",text); startActivity(i); } This way the intent is sent with Bundle data. – confused_at_times Apr 17 '13 at 12:18
  • I get the error 'The constructor Intent(new View.OnClickListener(){}, Class) is undefined' when i create the intent. – Karl Apr 17 '13 at 12:36
  • Use getBaseContext() as your Context for the first argument. Actually, post the code for the OnClickListener to be sure you are setting it up correctly. – confused_at_times Apr 17 '13 at 12:43