I have AlertDialog that lets user to pick up one of available choices. I have 7 choices and have separate array where 1 and 0 describe whether choice is valid or not. Then I do this :
public void createListAlertDialog() {
ListView list;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a Sampling Rate");
builder.setSingleChoiceItems(SampleRates_Items, SampleRates_Index, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
SampleRates_Index = item;
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { //Add the OK button
public void onClick(DialogInterface dialog, int which) {
PickFsDone = true;
}
}
);
AlertDialog alert = builder.create();
list = alert.getListView(); // *** I get crash on this line...
for (int i = 0; i < (SampleRates_Num); i++) { // index
if (SampleRates_Valid[i] == 0) {
// Disable choice in dialog
list.getChildAt(i).setEnabled(false);
} else {
// Enable choice in dialog
list.getChildAt(i).setEnabled(true);
}
}
alert.show();
}
I get crash in line marked with // *** ... What am I doing wrong? I must be missing something obvious... What I want to do is to disable choices that are marked with 0 in SampleRates_Valid[x]
.
UPDATE: Crash happens on other two lines with SetEnabled method. Here is crash log :
10-09 02:33:18.624: D/AndroidRuntime(7105): Shutting down VM
10-09 02:33:18.624: E/AndroidRuntime(7105): FATAL EXCEPTION: main
10-09 02:33:18.624: E/AndroidRuntime(7105): Process: processing.test.soundanalyzer, PID: 7105
10-09 02:33:18.624: E/AndroidRuntime(7105): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setEnabled(boolean)' on a null object reference
10-09 02:33:18.624: E/AndroidRuntime(7105): at processing.test.soundanalyzer.SoundAnalyzer.createListAlertDialog(SoundAnalyzer.java:995)
10-09 02:33:18.624: E/AndroidRuntime(7105): at processing.test.soundanalyzer.SoundAnalyzer$5.run(SoundAnalyzer.java:1014)
10-09 02:33:18.624: E/AndroidRuntime(7105): at android.os.Handler.handleCallback(Handler.java:815)
10-09 02:33:18.624: E/AndroidRuntime(7105): at android.os.Handler.dispatchMessage(Handler.java:104)
10-09 02:33:18.624: E/AndroidRuntime(7105): at android.os.Looper.loop(Looper.java:194)
10-09 02:33:18.624: E/AndroidRuntime(7105): at android.app.ActivityThread.main(ActivityThread.java:5534)
10-09 02:33:18.624: E/AndroidRuntime(7105): at java.lang.reflect.Method.invoke(Native Method)
10-09 02:33:18.624: E/AndroidRuntime(7105): at java.lang.reflect.Method.invoke(Method.java:372)
10-09 02:33:18.624: E/AndroidRuntime(7105): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
10-09 02:33:18.624: E/AndroidRuntime(7105): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)