Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R.xml.preferences cannot be found?

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:

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

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="Dev options">

        <CheckBoxPreference
            android:key="devtools"
            android:title="Enable clearing user"
            android:summary="Enable clearing user" />

    </PreferenceCategory>

</PreferenceScreen>

The java class file:

package com.myapp.prototype;

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

public class GCPreferencesActivity extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

Thanks for any help.

like image 646
gcl1 Avatar asked Apr 11 '12 02:04

gcl1


2 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 you 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 !).

like image 106
JFL Avatar answered Oct 17 '22 15:10

JFL


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

like image 39
Bruno Peres Avatar answered Oct 17 '22 16:10

Bruno Peres