我遇到了一个似乎出现在模拟器和个人可调试设备上的异常。
这是preferences.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header
android:title="sound"
android:summary=""
android:icon="@android:drawable/ic_media_ff"
android:fragment="com.example.app.activities.PreferencesActivity$SoundSettingsPreferenceFragment" />
</preference-headers>
这是preferences_sound 文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="First Category"
android:key="first_category">
<CheckBoxPreference
android:key="pref_cbx"
android:title="checkbox"
android:summary="checkbox summ"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>
这是相关的代码 Activity:
import java.util.List;
import android.annotation.SuppressLint;
import android.preference.PreferenceActivity;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import com.example.app.R;
@SuppressLint("NewApi")
public class PreferencesActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preferences, target);
}
public static class SoundSettingsPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_sound);
}
}
}
这是输出错误:
10-30 11:31:58.812: E/ActivityThread(9783): Failed to inflate
10-30 11:31:58.812: E/ActivityThread(9783): android.view.InflateException: Binary XML file line #3: Error inflating class preference-headers
...
10-30 11:31:58.812: E/ActivityThread(9783): Caused by: java.lang.ClassNotFoundException: android.preference.preference-headers
...