0

At the moment I have some test code that starts something like this:

    CheckBoxPreference cb1 = new CheckBoxPreference(this);
    CheckBoxPreference cb2 = new CheckBoxPreference(this);

What I want to achieve is an array of CheckBoxPreferences which I would expect to look something like this:

private CheckBoxPreference[] mFilterSubjects = new CheckBoxPreference(this)[24];

However, this generates an error "The type of the expression must be an array type but it resolved to CheckBoxPreference". The following code compiles correctly:

private CheckBoxPreference[] mFilterSubjects = new CheckBoxPreference[24];

However, if I try to do something with an element of the array, e.g. mFilterSubjects[0], I get a NullPointerException because there is no context.

How can I change my declaration to work properly, in other words, how do I declare each element of the array with context?

4

1 に答える 1

3

24 個の要素を保持する空の配列を作成しているだけなので、null ポインターを取得するのはそのためです。

private CheckBoxPreference[] mFilterSubjects = new CheckBoxPreference[24];

その配列に設定を追加する必要があります。

for(int i = 0; i < 24; i++){
    mFilterSubjects[i] = new CheckBoxPreference(this);
}
于 2012-09-24T12:00:12.313 に答える