我在我的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>
是文件路径恰好** **'/ RES/XML/preferences.xml'? –
是的确实如此 – Nathalie
您是否清理过该项目并重试? –