Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android tutorial error: R cannot be resolved to a variable

Tags:

android

http://developer.android.com/training/basics/firstapp/building-ui.html

I have been following this tutorial, but I have two errors, both "R cannot be resolved to a variable". I have made android apps (easy ones) in the past, and I remember this problem being fixed by checking whether or not I have an import R statement (which I don't) and whether or not the project has been cleaned before being built again (I cleaned it and I still get the error). I am at a loss as to what to do. Thanks!

*I also want to mention I did see the thread of the same title with 170 hits, and the solution of "delete the import R statement" does not apply to my problem (I don't think) Thanks again

All code is straight from the link above, but here it is for convenience

activity_my_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        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" />
</LinearLayout>

MyFirstActivity.java

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MyFirstActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_my_first, menu);
        return true;
    }


}

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<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="menu_settings">Menu Settings</string>
</resources>

edit: When I clean the project, I get this message in the Console:

[2012-06-29 11:12:38 - MyFirstApp] W/ResourceType( 6140): Bad XML block: header size 91 or total size 0 is larger than data size 0 [2012-06-29 11:12:38 - MyFirstApp] C:\Users\zhong\workspace\eclipse\MyFirstApp\res\menu\activity_my_first.xml:2: error: Error: No resource found that matches the given name (at 'title' with value '@string/menu_settings').

edit: added a line to layout xml file <string name="menu_settings">Menu Settings</string> Fixed cleaning project errors, but I still can't run the project from the two R errors.

like image 897
SwimBikeRun Avatar asked Dec 13 '22 00:12

SwimBikeRun


1 Answers

I'm working my way through the same example, and had the same (or very similar) problem.

Finally I noticed that there was a tiny little red x on the manifest.xml. Sure enough, it was complaining about this:

android:label="@string/title_activity_hello_world" >

So I added:

<string name="title_activity_hello_world">Hello World</string>

to strings.xml and now it works.

like image 107
Omri Barel Avatar answered Feb 23 '23 10:02

Omri Barel