2014-05-21 173 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>