Take the 2-minute tour ×

I'm trying to set up some simple preferences for an Android app, but can't get past this error: "xml cannot be resolved or is not a field." I have cleaned, refreshed, restarted eclipse, and danced the jig - but I can't shake the error. What am I doing wrong?

The preferences.xml file:



        android:title="Dev options">

            android:title="Enable clearing user"
            android:summary="Enable clearing user" />



The java class file:

package com.myapp.prototype;

import android.os.Bundle;
import android.preference.PreferenceFragment;

public class GCPreferencesActivity extends PreferenceFragment {

    public void onCreate(Bundle savedInstanceState) {

        // Load preferences from XML resource
        addPreferencesFromResource(R.xml.preferences);    // error on this line ??

Thanks for any help.

share|improve this question
Where's your preferences file located? It should be: <project>/res/xml/preferences.xml. –  MH. Apr 11 '12 at 2:25
and import com.myapp.prototype.R; –  Its not blank Apr 11 '12 at 2:57
Roger that, thanks. –  gcl1 Apr 11 '12 at 11:22
Had that has well. Namespace is a good thing, but it's a shame there are two "R" classes in the Android framework... Could have been avoided. –  tiktak Aug 6 '12 at 12:19
add comment

4 Answers

I think you have missed an import (as said Vincent in comments). you should add:

import com.myapp.prototype.R;

The error also appears if your put in your code:

import android.R;

which is confusing because R is resolved but has no attribute that you defined in your XML file (of course, it is not the good R !).

share|improve this answer
Yeah, I resolved this after Vincent's answer via comments. I just had preferences.xml in the wrong directory (under res/layout instead of res/xml). Thanks for the follow up. –  gcl1 Oct 1 '12 at 14:34
add comment

You must have a folder inside your res/ directory called res/xml and inside it have your .xml file.

share|improve this answer
add comment

For future visitors: I had the same problem. And One of the video I went through from lynda.com said that , the folder XML will be created with a file name.xml if we don't have one. But it dint' happen the way it was explained. I had to manually create a folder called "XML" under the "RES" folder and drag that xml file under it and error disappeared.

share|improve this answer
add comment

I had the problem because I copied/pasted the xml from another project. Create the xml in the project with File=>new=>Android xml and then copy paste the xml code from the other project to the xml you just created.

share|improve this answer
add comment

Your Answer


