gpt4 book ai didi

android - 没有 fragment 的首选项标题标题?

转载 作者:行者123 更新时间:2023-11-29 21:16:41 24 4
gpt4 key购买 nike

我正在编写一个自定义的 Android 首选项屏幕,它崩溃了 b/c 当标题没有 fragment 时它期待一个 fragment ——我只想有一个标题,它是给定集合的标题部分。尝试搜索,无济于事,如果这是一个骗局,我们深表歉意。

pref_headers.xml

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header android:id="@+id/headerStaffGeneral" android:title="@string/staff_general" /> <!-- This causes a crash as name == null -->
<header
android:id="@+id/headerSettings"
android:fragment="com.example.ex.prefs.SettingsPreferencesFragment"
android:title="@string/staff_manager_settings"
android:icon="@drawable/ic_action_settings"
/>
</preference-headers>

如果需要包含任何其他代码,我可以包含它,但我的问题的本质是我是否应该用其他东西替换崩溃的 HEADER 标记。

更新:

LogCat 输出

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ex/com.example.ex.prefs.PrefsActivity}: java.lang.NullPointerException: name == null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: name == null
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:491)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.Fragment.instantiate(Fragment.java:582)
at android.preference.PreferenceActivity.switchToHeaderInner(PreferenceActivity.java:1245)
at android.preference.PreferenceActivity.switchToHeader(PreferenceActivity.java:1278)
at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:647)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
... 11 more

PrefsActivity.java

package com.example.ex.prefs;

import java.util.ArrayList;
import java.util.List;

import android.preference.PreferenceActivity;
import android.widget.ListAdapter;

import com.example.ex.R;

public class PrefsActivity extends PreferenceActivity {

private List<Header> mHeaders;

protected void onResume() {
super.onResume();

setTitle(R.string.staff_manager_settings);

if (getListAdapter() instanceof PrefsHeaderAdapter)
((PrefsHeaderAdapter) getListAdapter()).resume();
}

protected void onPause() {
super.onPause();
if (getListAdapter() instanceof PrefsHeaderAdapter)
((PrefsHeaderAdapter) getListAdapter()).pause();
}

public void onBuildHeaders(List<Header> target) {
// Called when the settings screen is up for the first time
// we load the headers from our xml description

loadHeadersFromResource(R.xml.pref_headers, target);

mHeaders = target;
}

public void setListAdapter(ListAdapter adapter) {
int i, count;

if (mHeaders == null) {
mHeaders = new ArrayList<Header>();
// When the saved state provides the list of headers,
// onBuildHeaders is not called
// so we build it from the adapter given, then use our own adapter

count = adapter.getCount();
for (i = 0; i < count; ++i)
mHeaders.add((Header) adapter.getItem(i));
}

super.setListAdapter(new PrefsHeaderAdapter(this, mHeaders));
}

}

如果从 pref_headers.xml 文件中删除第一个 HEADER 标记,则不会显示该错误。

最佳答案

你所拥有的应该可以工作,但也许你需要一个 android:id值(value)。如果您查看“设置”应用的首选项 header ,您会看到如下内容:

<preference-headers
xmlns:android="http://schemas.android.com/apk/res/android">


<!-- WIRELESS and NETWORKS -->
<header android:id="@+id/wireless_section"
android:title="@string/header_category_wireless_networks" />

<!-- Wifi -->
<header
android:id="@+id/wifi_settings"
android:fragment="com.android.settings.wifi.WifiSettings"
android:title="@string/wifi_settings_title"
android:icon="@drawable/ic_settings_wireless" />

...

所以,他们似乎在使用 <header>没有android:fragment用于标题列表中的节标题。

关于android - 没有 fragment 的首选项标题标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21238259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com