简体   繁体   中英

Android tutorial error: edit_message cannot be resolved or is not a field

I tried to follow the android tutorial but after I try to run the code this error appears. Here is my MainActivity.

    package com.example.menuexample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE="com.example.menuexample.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void sendMessage(View view){ 
        Intent intent = new Intent(this, DisplayMessageActivity.class); 
        EditText editText= (EditText) findViewById(R.id.edit_message);  --HERE
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent); 
    }


}

I have got in the activity_main :

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" >

</EditText>

Also , strings.xml has:

<string name="edit_message">Test</string>

Change

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

to

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

because you have declared Edittext with editText1 id in xml layout but you are trying to find control id which is not exist

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