简体   繁体   中英

Android Tutorial: edit_message cannot be resolved or is not a field

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM