Reputation: 11
this is the code in MyActivity.java :
public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
/** Called when the user clicks the Send button */
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);
}
}
It says it cannot resolve the symbol 'edit_message' (see 6th line). Answers to this problem were to add this string to strings.xml, but it is already there, and declared just fine:
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello world!</string>
</resources>
The error message in Gradle Build is: Error:(21, 57) error: cannot find symbol variable edit_message
Why can't it find this string?
I've been staring at this for quite a while and almost ashamed I can't solve it.. I mean, it's a tutorial for crying out loud. Any help would be apreciated very much!
Upvotes: 0
Views: 609
Reputation: 62
If you are trying to access a resource so:
String s = context.getResources().getString(id);
and the id is: R.id.edit_message
context can be replaced by this.
or getBaseContext()
.
By to access an EditText
it needs to be declared in the Layout' XML file (or declared at run time).
Upvotes: 0
Reputation: 3384
Your code is looking for a ´EditText´ element with name "edit_message" (not for a String with this name), so your layout xml file should contain EditText
element with id
edit_message
<EditText android:id="@+id/edit_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Upvotes: 1