0

I am taking the Android Tutorial and learning how to make apps, slowly but surely.

I have this in my Mainactvitiy:

public final static String EXTRA_MESSAGE = "com.mfr.firstapp.MESSAGE";

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

And this in my Fragment_main.xml

<EditText 
android:id="@+id/edit_messsage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" 
android:layout_weight="1">
</EditText>

Yet I am getting an error on this line

EditText editText = (EditText) findViewById(R.id.edit_message);

Telling me that "edit_message cannot be resolved or is not a field" Why would this be?

Regards

Matt

Matthew
  • 301
  • 5
  • 18

1 Answers1

0

The "edit_message" is in the Fragment's layout, so this code won't find it:

EditText editText = (EditText) findViewById(R.id.edit_message);

because it will look inside the layout that has been set as the contentView in the activity.

You are probably following an older tutorial that was done before the adt started to automatically generate a Fragment after creating a new Project. (which confuses many beginners)

donfuxx
  • 11,277
  • 6
  • 44
  • 76
  • Okay, thanks very much, I'll try and find another tutorial. Any suggestions for good, reliable and up-to-date tutorials? – Matthew May 04 '14 at 18:44
  • 2
    The tutorial is not necessarily bad, because that has changed a couple of months or so. Moreover, the generated code is quite silly anyway. I suggest first learn how to do an UI without fragments, then later learn fragments. Here is explained how you get rid of generated fragment: http://stackoverflow.com/a/22482259/2399024 – donfuxx May 04 '14 at 18:55