自定义 Android PreferenceFragment 不起作用

发布于 2025-01-01 22:31:29 字数 5259 浏览 1 评论 0原文

目前,对于我的 2.x 首选项屏幕,在每个 PreferenceActivity 中,我添加行 setContentView(R.layout.activity_preferences); 以在我的活动中拥有自定义布局。

Activity_preferences.xml 布局文件如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout style="@style/TitleBar">
        <ImageButton style="@style/TitleBarAction"
            android:src="@drawable/ic_title_home"
            android:onClick="onHomeClick" />

        <ImageView style="@style/TitleBarSeparator" />
        <eu.vranckaert.worktime.utils.view.CustomTextView
                style="@style/TitleBarText" android:text="@string/lbl_preferences_title"/>
    </LinearLayout>

    <ListView android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:id="@android:id/list"/>

</LinearLayout>

对于我的 3.x 及更高版本的应用程序,我根据此处的示例构建首选项: http://developer.android.com/reference/android/preference/PreferenceActivity.html

为了将相同的布局应用于我的 3.x 应用程序,我尝试在 3.x PreferenceActivity 中添加 setContentView(..)

在手机上的结果:这在第一个活动上工作正常,然后在加载片段(其中包含真正的首选项)时,我的样式未应用

在平板电脑上的结果:崩溃:

    ERROR/AndroidRuntime(2208): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.vranckaert.worktime/eu.vranckaert.worktime.activities.preferences.PreferencesICSActivity}: java.lang.IllegalArgumentException: No view found for id 0x10202d4 for fragment TimeRegistrationsPreferencesFragment{4101ed38 #0 id=0x10202d4}
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    at android.app.ActivityThread.access$600(ActivityThread.java:123)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    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:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.IllegalArgumentException: No view found for id 0x10202d4 for fragment TimeRegistrationsPreferencesFragment{4101ed38 #0 id=0x10202d4}
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:822)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
    at android.app.BackStackRecord.run(BackStackRecord.java:622)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
    at android.app.Activity.performStart(Activity.java:4474)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
    ... 11 more

当我尝试使用 getActivity().setContentView(R.layout.activity_preferences); 结果是: 对于手机:活动仍然显示良好,当进入类别时它崩溃:

    ERROR/AndroidRuntime(1320): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.vranckaert.worktime/eu.vranckaert.worktime.activities.preferences.PreferencesICSActivity}: java.lang.IllegalArgumentException: No view found for id 0x10202d4 for fragment DateTimePreferencesFragment{412a0d78 #0 id=0x10202d4}
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    at android.app.ActivityThread.access$600(ActivityThread.java:123)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    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:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.IllegalArgumentException: No view found for id 0x10202d4 for fragment TimeRegistrationsPreferencesFragment{412a0d78 #0 id=0x10202d4}
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:822)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
    at android.app.BackStackRecord.run(BackStackRecord.java:622)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
    at android.app.Activity.performStart(Activity.java:4474)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
    ... 11 more

对于平板电脑:与以前一样崩溃...

我应该如何将此布局文件应用到我的片段活动...?或者我应该创建一个新的布局文件?

Currently for my 2.x preference screens, in every PreferenceActivity I add the line setContentView(R.layout.activity_preferences); to have a custom layout in my activities.

The activity_preferences.xml layout file looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout style="@style/TitleBar">
        <ImageButton style="@style/TitleBarAction"
            android:src="@drawable/ic_title_home"
            android:onClick="onHomeClick" />

        <ImageView style="@style/TitleBarSeparator" />
        <eu.vranckaert.worktime.utils.view.CustomTextView
                style="@style/TitleBarText" android:text="@string/lbl_preferences_title"/>
    </LinearLayout>

    <ListView android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:id="@android:id/list"/>

</LinearLayout>

For my 3.x and up application I am building the preferences according to the example over here: http://developer.android.com/reference/android/preference/PreferenceActivity.html.

In order to apply the same layout to my 3.x app, I tried was to add the setContentView(..) in the 3.x PreferenceActivity.

Result on phone: this works fine on the first activity, then when loading a fragment (with the real preferences in them) my style is not applied

Result on tablet: crash:

    ERROR/AndroidRuntime(2208): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.vranckaert.worktime/eu.vranckaert.worktime.activities.preferences.PreferencesICSActivity}: java.lang.IllegalArgumentException: No view found for id 0x10202d4 for fragment TimeRegistrationsPreferencesFragment{4101ed38 #0 id=0x10202d4}
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    at android.app.ActivityThread.access$600(ActivityThread.java:123)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    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:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.IllegalArgumentException: No view found for id 0x10202d4 for fragment TimeRegistrationsPreferencesFragment{4101ed38 #0 id=0x10202d4}
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:822)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
    at android.app.BackStackRecord.run(BackStackRecord.java:622)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
    at android.app.Activity.performStart(Activity.java:4474)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
    ... 11 more

When I try to apply the layout file on my fragments using getActivity().setContentView(R.layout.activity_preferences); the result is:
for phone: the activity is still shown fine, when going into a category it crashes:

    ERROR/AndroidRuntime(1320): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.vranckaert.worktime/eu.vranckaert.worktime.activities.preferences.PreferencesICSActivity}: java.lang.IllegalArgumentException: No view found for id 0x10202d4 for fragment DateTimePreferencesFragment{412a0d78 #0 id=0x10202d4}
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    at android.app.ActivityThread.access$600(ActivityThread.java:123)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    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:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.IllegalArgumentException: No view found for id 0x10202d4 for fragment TimeRegistrationsPreferencesFragment{412a0d78 #0 id=0x10202d4}
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:822)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
    at android.app.BackStackRecord.run(BackStackRecord.java:622)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
    at android.app.Activity.performStart(Activity.java:4474)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
    ... 11 more

for tablet: same crash as before...

How should I apply this layout file to my fragment activity...? Or should I create a new layout file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文