2014-05-21 225 views
-2

我在我的preferences.xml文件夾中創建了一些偏好設置,但是當我嘗試在Settings.java類中設置它時,我在addPreferencesFromRecource(R.xml.preferences)的xml下獲得了一個紅色下劃線。我發現這個話題R.xml.preferences cannot be found?有人有我完全相同的問題,並建議包括一個導入(在我的情況下,它是導入com.example.anneholmes.R),並檢查preferences.xml的位置。 我的preferences.xml位於res/xml文件夾中,我做了導入;然而,它並沒有解決我的錯誤。我該如何解決這個錯誤?如何解決R.xml.preferences錯誤?

這是Settings.java代碼

package com.example.anneholmes; 

import android.app.Activity; 
import com.example.anneholmes.R; //did import 
import android.os.Bundle; 
import android.preference.Preference; 
import android.preference.Preference.OnPreferenceClickListener; 
import android.preference.PreferenceFragment; 

public class Settings extends Activity { 
//code 
     . 
     . 
     . 

} 
public static class PrefsFragment extends PreferenceFragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); // error 
     Preference submitPref = (Preference)findPreference("submitPref"); 
     submitPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){ 

        //code 
         . 
         . 
         . 

這是從的preferences.xml

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 
<PreferenceCategory android:title="PERSONAL INFORMATION"> 
    <EditTextPreference 
     android:key="firstName" 
     android:title="First Name" 
     android:summary="Enter your First Name" 
     android:dialogTitle="Enter your first name"> 

    </EditTextPreference> 

    <EditTextPreference 
     android:key="lastName" 
     android:title="Last Name" 
     android:summary="Enter your Last Name" 
     android:dialogTitle="Enter your last name"> 

    </EditTextPreference> 

    <EditTextPreference 
     android:key="email" 
     android:title="Email address" 
     android:summary="Enter your email address" 
     android:dialogTitle="Enter your email address"> 

    </EditTextPreference> 

    </PreferenceCategory> 

    <PreferenceCategory android:title="Colors"> 
     <ListPreference 
      android:entries="@array/colors" 
      android:entryValues="@array/colorSelected" 
      android:key="textcolor" 
      android:summary="Choose the Font Color" 
      android:title="Foreground Color"/> 
     <ListPreference 
      android:entries="@array/colors" 
      android:entryValues="@array/colorSelected" 
      android:key="color" 
      android:title="Background Color" 
      android:summary="Choose the Background Color"/> 
    </PreferenceCategory> 

     <Preference 
      android:title="Save this Information" 
      android:key="submitPref"/> 


    </PreferenceScreen> 
+2

是文件路徑恰好** **'/ RES/XML/preferences.xml'? –

+0

是的確實如此 – Nathalie

+0

您是否清理過該項目並重試? –

回答

2

XML你永遠不會關閉<PreferenceScreen>的標籤,所以這裏的XML是無效的。

由於它是無效的,它將不會編譯,因此將不會在R中嘗試從Java代碼引用的int。

這就是爲什麼它說它不存在。這在任何地方都沒有得到很好的解釋,所以我以前必須自己去找。

+0

。它現在有 – Raghunandan

+0

你的項目在其他地方有任何錯誤嗎?在XML文件中如果有錯誤,R.java將無法編譯,並且會在你修復它之前破壞大量的東西。 – Mgamerz

+0

我實際上有三個錯誤在我的MainActivity.java – Nathalie

0

加入這一行清單文件:

<activity android:name=".preferences"></activity>