0

I'm a newbie with Android dev. I have a simple "learning" app with a very simple layout. The layout shows only one warning (hardcoded string ... should should use @string resource) which I doubt is related to the problem. Yet, in my activity code, I have the error "R cannot be resolved to a variable".

Here's the java:

package com.example.treefields;

import android.app.Activity;
import android.os.Bundle;

public class TreeFieldsActivity extends Activity
{
  public void onCreate (Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContextView (R.layout.activity_tree_fields);
  }
}

And, here's the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textMultiLine"
        android:text="multiline text" />

</LinearLayout>

Any ideas? Thanks.

5
  • 2
    Clean and rebuild your project. Report back with errors if R is still not generated. Commented Jul 30, 2012 at 20:51
  • 2
    The error can be in any XML file, not just your layout. Open Eclipse's Package Explorer Window and follow the error icons to wherever the errors are or use the Problems Window. Commented Jul 30, 2012 at 20:52
  • 1
    Also try deleting the R.java file. It will re-build itself and then should be correct. Commented Jul 30, 2012 at 20:53
  • 1
    Add import com.example.treefields.R; Commented Jul 30, 2012 at 21:09
  • Don't know what that android:ems is perhaps thats syntactically incorrect therefore your R file isn't being built. Commented Jul 30, 2012 at 21:24

2 Answers 2

2

It doesn't look like you are even including R. You need to include the resources package as such:

import your.package.name.R;

The lint string warning is likely unrelated as you say.

Sign up to request clarification or add additional context in comments.

2 Comments

This appears to be the problem, which I don't yet fully understand. I added import com.example.textfields.R. I have two packages - I am learning about switching between activities, so I started with TextFieldsActivity (which worked), then added TreeFieldsActivity. The former (tree fields) was initially autogenerated and does not have an import for the "R" file. Yet, it built and ran fine. So, why does the second package need the import?
R is built in the package defined in your manifest. You should import this in all files where you access the resources. Possibly the activity that worked without the include wasn't using the R package? BTW your Activities don't need to be in different packages.
1

If you're using Eclipse, you can always use Ctrl+Shift+o to let the IDE figure out what to import, but like Bostwickenator said, you should make sure you include the R generated by your package, and not android.R on accident (as that would not resolve to any layout elements you created, but rather some Android common stuff).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.