请在这里帮助我,我在过去的两天里碰到了砖墙而且无法弄明白。
我有一个简单的PreferenceFragment,并希望从一个Activity中充气。
这是res / xml / preferences.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="preferences">
<PreferenceCategory
android:title="@string/notification_basic"
android:key="notification">
<CheckBoxPreference
android:key="enable_notification"
android:summary="@string/enable_notification"
android:title="@string/notification_title"
android:defaultValue="false">
</CheckBoxPreference>
<CheckBoxPreference
android:key="enable"
android:summary="@string/enable_instant_notification"
android:title="@string/notify_me_instantly"
android:dependency="enable_notification"
android:defaultValue="false">
</CheckBoxPreference>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/sign_up_notify_me_where_section"
android:key="locations">
<MultiSelectListPreference
android:dialogTitle="@string/sign_up_notify_me_where_title"
android:key="location"
android:summary="@string/sign_up_notify_me_where"
android:title="@string/sign_up_notify_me_where_title"
android:entries="@array/locations"
android:entryValues="@array/locations_values"
android:defaultValue="@array/empty_array"
android:dependency="enable_notification"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/sign_up_notify_me_type_section"
android:key="types">
<MultiSelectListPreference
android:dialogTitle="@string/sign_up_notify_me_type_title"
android:key="type"
android:summary="@string/sign_up_notify_me_type"
android:title="@string/sign_up_notify_me_type_title"
android:entries="@array/types"
android:entryValues="@array/types_values"
android:defaultValue="@array/empty_array"
android:dependency="enable_notification"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/sign_up_notify_me_who_section"
android:key="units">
<MultiSelectListPreference
android:dialogTitle="@string/sign_up_notify_me_who_title"
android:key="units"
android:summary="@string/sign_up_notify_me_who"
android:title="@string/sign_up_notify_me_who_title"
android:entries="@array/units"
android:entryValues="@array/units_values"
android:defaultValue="@array/empty_array"
android:dependency="enable_notification"
/>
</PreferenceCategory>
</PreferenceScreen>
活动:
public class HomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, new SignUpPreferenceFragment());
fragmentTransaction.commit();
}
}
活动的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/home_layout">
<FrameLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/fragment_container"
/>
</RelativeLayout>
最后是堆栈跟踪:
04-05 20:11:07.641: ERROR/AndroidRuntime(5731): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.soid.client.activities/com.soid.client.activities.HomeActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class PreferenceScreen
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access$600(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5520)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class PreferenceScreen
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.soid.client.activities.SignUpPreferenceFragment.onCreateView(SignUpPreferenceFragment.ja va:18)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:831)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1037)
at android.app.BackStackRecord.run(BackStackRecord.java:635)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399)
at android.app.Activity.performStart(Activity.java:5075)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
... 11 more
Caused by: java.lang.ClassNotFoundException: android.view.PreferenceScreen
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.view.LayoutInflater.createView(LayoutInflater.java:552)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
... 20 more
为什么在android.view中搜索PreferenceScreen?我完全失去了,请求帮助我。
编辑:
我忘了粘贴实际的PreferenceFragment:
public class SignUpPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.xml.preferences, container, false);
谢谢!
答案 0 :(得分:11)
您需要删除onCreateView
中的SignUpPreferenceFragment
功能,因为您无法使用inflater扩充首选项。
这就是为什么你收到错误,指出有一个错误导致PreferenceScreen
膨胀,因为它不理解xml中的那个标记/类。
preferences.xml
的{{1}}由addPreferencesFromResource
onCreate
完成。
答案 1 :(得分:1)
你必须有一个类,你可以像这样膨胀preference.xml:
public static class SignUpPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
答案 2 :(得分:0)
将首选项主题添加到styles.xml
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>