我在我的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
您是否清理過該項目並重試? –