編寫 XML 資源檔(如 Preference、Layout、Menu)時,常遇 「Error parsing XML: unbound prefix」 錯誤,根因是根元素缺少命名空間宣告。
<PreferenceScreen>
<CheckBoxPreference android:key="sync" android:title="同步" />
</PreferenceScreen>
報錯:unbound prefix 指 android: 前綴未綁定命名空間。
在根元素添加 xmlns:android 宣告:
<PreferenceScreen xmlns:android="https://schemas.android.com/apk/res/android">
<CheckBoxPreference android:key="sync" android:title="同步" />
</PreferenceScreen>
xmlns:prefix="URI" 將前綴綁定至 URI;android:layout_width、android:id 等)均屬 https://schemas.android.com/apk/res/android 命名空間;layout/、menu/、xml/(Preference)、values/ 等;xmlns:app="https://schemas.android.com/apk/res-auto"。