首先,道歉这很长,但我试图提供所有相关信息。
我有一个班级,其中包含用于解码NOTAM中“Q”线部分内容的信息(发布NOTAM以警告飞机驾驶员在途中或在特定位置发生的任何危险。定义“问“行here)。有许多“类别”,每个类别包含许多“主题”。我有代码过滤掉(删除)与特定类型的飞行员(滑翔机等)无关的某些主题。在我的首选项中,用户可以指定应删除或不应删除特定主题。每个主题都有默认设置。
我正在尝试初始化所有类别和所有主题的首选项,但是没有设置第一个类别之外的首选项,当我尝试重置默认值时导致空指针异常。
每个类别都会重复使用主题。这是设置复选框的位置:
private CheckBoxPreference[] mFilterSubjects = new CheckBoxPreference[MAX_FILTER_SUBJECTS];
// In onCreate:
// Add the preferences to the subject array
for (int i = 0; i < MAX_FILTER_SUBJECTS; i++)
{
mFilterSubjects[i] = new CheckBoxPreference(this);
}
这是我初始化过滤器的地方:
// Initialize filters for type.
// Set filters for each category
private void initFilters(int type)
{
int cat;
// Non-relevant code removed
for (cat = 0; cat < Constants.k_nNumFilterCategories; cat++)
{
setFiltersForCategory(type, cat);
}
}
设置过滤器的代码。注意:构建Qline数组的文本source file是线性的,首先定义类别的行,然后是每个类别中每个主题的一组行。因此需要if语句。
private void setFiltersForCategory(int type, int cat)
{
int i, j;
boolean canDelete;
Qline qline;
j = 0;
for (i = 0; i < Spine.mQlines.size(); i++)
{
qline = (Qline)Spine.mQlines.get(i);
// Only looking for subject entries
// for the current category
if ((qline.getQtype() == 1) && (qline.getCatRef() == cat))
{
mFilterSubjects[j].setTitle(qline.getText());
mFilterSubjects[j].setKey("typ" + type + "cat"+ cat + "sub" + j);
mFilterSubjects[j].setDefaultValue(qline.getDefault(type));
canDelete = qline.getAuto(type);
mFilterSubjects[j].setEnabled(canDelete);
mFilterSubjects[j].setSummary(canDelete ? "" : "This subject cannot be deleted");
mFilterSubjects[j].setOrder(j);
mFilterShow.addPreference(mFilterSubjects[j]);
j++;
}
}
}
很明显,我正在为每个类别重复使用mFilterSubjects数组,但是期望在这里创建所有类别中所有主题的所有键。 显然,他们不是,因为当我尝试设置默认值并且类别从0(所有ok)移动到1(键“typ0cat1sub0”时,我在以下代码中的行cb.setChecked处得到NullPointerException “)。
// Set defaults for current filter type
// (all categories)
private void setFilterDefaults()
{
Qline qline;
int i, j, k;
int type = Integer.parseInt(mFilterType.getValue());
CheckBoxPreference cb;
String key;
// Non-relevant code removed
for (i = 0; i < Constants.k_nNumFilterCategories; i++)
{
k = 0;
for (j = 0; j < Spine.mQlines.size(); j++)
{
qline = (Qline)Spine.mQlines.get(j);
// Only looking for subject entries
// for the current category
if ((qline.getQtype() == 1) && (qline.getCatRef() == i))
{
key = "typ" + type + "cat" + i + "sub" + k++;
cb = (CheckBoxPreference)getPreferenceScreen().findPreference(key);
cb.setChecked(qline.getDefault(type));
}
}
}
}
如果相关,则定义首选项屏幕mFilterShow(key filterShow)的xml位于:
<PreferenceCategory android:title="Filters">
<PreferenceScreen android:key="FilterScreen"
android:title="Filters" android:summary="Click to change filter settings">
<ListPreference android:title="Filter type"
android:summary="Set to: Gliding"
android:key="filterType"
android:defaultValue="0"
android:entries="@array/filterTypeOptions"
android:entryValues="@array/filterTypeValues" />
<Preference android:title="Set defaults for Gliding"
android:key="filterDefaults" />
<CheckBoxPreference android:title=""
android:summary="Include Aerodrome Notams"
android:defaultValue="false" android:key="filterIncludeAerodrome" />
<CheckBoxPreference android:title=""
android:summary="Delete night-time Notams"
android:defaultValue="true" android:key="filterDeleteNighttime" />
<ListPreference android:title="Select category to change"
android:summary="Set to: Airspace organisation"
android:key="filterCategory"
android:defaultValue="0"
android:entries="@array/filterCategoryOptions"
android:entryValues="@array/filterCategoryValues" />
<PreferenceScreen android:title="Choose subjects to delete"
android:summary="Click to show subjects for:\n(category goes here)"
android:key="filterShow">
</PreferenceScreen>
</PreferenceScreen>
</PreferenceCategory>
结论:为什么不创建所有首选项?