0

I've a problem with some objects in my Android application. I'm pretty newbie so the error might be stupid but I can't see it.

Here the code:

activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/main_page"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/main_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@str_listen"
        android:id="@+id/button_send"
        android:onClick="sendMessage" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@str_info"
        android:id="@+id/id_info" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@str_credits"
        android:id="@+id/id_credits" />

</LinearLayout>

MainActivity.java

package com.example.cartoonia;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {    

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

        Button btn_listen = (Button) findViewById(R.id.id_listen);
        Button btn_credit = (Button) findViewById(R.id.id_credits);
        Button btn_info = (Button) findViewById(R.id.id_info);

    }

    /** Called when the user touches the button */
    public void sendMessage(View view) {
        // Do something in response to button click
        TextView tv = (TextView) findViewById(R.id.main_TV);
    }

    @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;
    }    
}

I'm using eclipse and usually, when I add an item in the xml file, its id becomes accessible immediately.

The id of the linear layout is accessible, the ids of the other elements added in a second moment aren't. The error shown is, for example, *id_info cannot be resolved or is not a field*

How can I fix it?

Max Markson
  • 800
  • 1
  • 19
  • 34
  • Have you tried cleaning up the whole project and rebuilding it afterwards? – nKn Feb 02 '14 at 13:51
  • I have clean the project...now I have an error on R and I have to import android.R. If I do so I have an error on ALL ids and I can't build because I have errors in project. @VladSchnakovszki I've already saved the xml file, I'm newbie but not so newbie! – Max Markson Feb 02 '14 at 13:57
  • Do not import android.R; you want yourpackagename.R instead. – Luis Feb 02 '14 at 13:58
  • After the clean my file R disappear and, even after a build, doesn't appear. Even the strings.xml file has disappear. – Max Markson Feb 02 '14 at 14:06

1 Answers1

2

You have errors in your xml layout, so AAPT cannot generate a new R.class file for you. Once you have fixed the errors, the ids will be resolved just fine.

Your errors are the string references. You cannot just use @string_name, but have to use @string/string_name.

E.g. this:

android:text="@str_listen"

should be

android:text="@string/str_listen"
FD_
  • 12,947
  • 4
  • 35
  • 62