当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Dynamic preferences not being set - what am I doing wrong?

问题描述:

First off, apologies that this is so long, but I have tried to provide all relevant information.

I have a class that holds information which is used to decode part of the contents of the "Q" line in a NOTAM (NOTAMs are issued to alert aircraft pilots of any hazards en route or at a specific location. Definition of "Q" line here). There are a number of "categories" each containing a number of "subjects". I have code that filters out (deletes) certain subjects which are not relevant to a particular type of pilot (gliders, etc.). In my preferences, the user can specify that a particular subject should or should not be deleted. Each subject has a default setting.

I am trying to initialize the preferences for all categories and all subjects, but the preferences beyond the first category are not set up, resulting in a null pointer exception when I try to reset defaults.

Subjects are re-used for each category. This is where the check boxes are set up:

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);

}

This is where I initialize the filters:

// 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);

}

}

Code to set filters. Note: the text source file from which the Qline array is built is linear, with lines defining the categories first, then a set of lines for each subject in each category. Hence the need for the if statement.

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++;

}

}

}

As will be apparent, I am re-using the mFilterSubjects array for each category, but was expecting all the keys for all subjects in all categories to be created here. Patently, they are not, because I get a NullPointerException at the line cb.setChecked in the following code when I try to set defaults and the category moves from 0 (all ok) to 1 (key "typ0cat1sub0").

// 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));

}

}

}

}

If it's relevant, the xml that defines the preference screen mFilterShow (key filterShow) is here:

<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>

Bottom line: why don't all the preferences get created?

分享给朋友:
您可能感兴趣的文章:
随机阅读: