2

So I have followed the tutorial on https://developer.android.com/training/basics/firstapp/index.html trying to build an app, and I've written all the code now but have got a few errors. I am using Eclipse if that matters.

activity_main.xml : I get errors on @string/edit_message - "error: Error: No resource found that matches the given name (at 'hint' with value '@string/edit_message')."

and @string/button_send - "error: Error: No resource found that matches the given name (at 'text' with value '@string/button_send')."

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />

</LinearLayout>

And then there's MainActivity.java - Here I get an error on EditText editText = (EditText) findViewById | this part (R.id.edit_message); | saying "edit_message cannot be resolved or is not a field"

    package com.example.myfirstapp;

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

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/** 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);


    // Do something in response to button

}
}

Next, on strings.xml, I get this: "Multiple annotations found at this line: - error: Error parsing XML: not well-formed (invalid token)" next to _settings">Settings the code:

    <?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">My First App</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_display_message">Mitt meddelande</string>

</resources>
_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>

And then on activity_display_message.xml on @string/hello_world I get " error: Error: No resource found that matches the given name (at 'text' with value '@string/hello_world'). "

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

And finally, on DisplayMessageActivity.java, on (R.layout. | this part - activity_display_message) | I get " activity_display_message cannot be resolved or is not a field "

    package com.example.myfirstapp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;

public class DisplayMessageActivity extends Activity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

Sorry for a very long and possibly messy post, but I am new to this and trying to learn so I would really like to know what is up with these errors!

Thanks in advance!

ul09lu09
  • 49
  • 4

5 Answers5

2

mistake in Button click method,s is not small it's capital .

public void SendMessage(View view) {
            ^

https://stackoverflow.com/a/11970085/2337837

Community
  • 1
  • 1
Zied R.
  • 4,964
  • 2
  • 36
  • 67
2

You have forgotten to set the ID of this element:

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

Adding android:id="@+id/edit_message" should solve your problem.

Manitoba
  • 8,522
  • 11
  • 60
  • 122
1

This part right here is screwing everything up.

</resources>
_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>

When you have bad XML in any of your files, none of your XML files get included correctly so you get a ton of errors. I'm not sure what you're trying to do with _settings">Settings</string> but you either have to fix that or remove it.

EDIT: Actually, looking at the tutorial, you're gonna have to remove the first </resources> and then the line I previously mentioned. It should work after that.

Embattled Swag
  • 1,479
  • 12
  • 23
  • Replaced what I had wrote with what the guy on here said, but it only fixed the errors in that file – ul09lu09 Feb 26 '14 at 21:37
1

you should add this in your ressources

<string name="edit_message">Something here</string>
<string name="button_send">Something here</string>

Edit :and fix this:

Settings

murielK
  • 1,000
  • 1
  • 10
  • 21
1

you String ressource file should look like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">My First App</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_display_message">Mitt meddelande</string>
<string name="edit_message">edit msg</string>
<string name="button_send">send</string>

</resources>

you have missed some items and had formatting bugs

donfuxx
  • 11,277
  • 6
  • 44
  • 76