我在Android设备中为AlertDialog动态构建字符串数组。 我创建了这样的字符串数组1:
final int SAMPLERATES_NUM = 9;
String[] SampleRates_Str = new String[SAMPLERATES_NUM];
SampleRates_Str[ValidSampleRate_Index] = Integer.toString(SampRate);
现在我只需通过以下方式创建字符串数组:
String[] SampleRates_Str1 = {"48000", "44100", "32000", "22050", "16000", "11025", "8000", "4000"};
现在我调用AlertDialog并显示这两个数组(当时一个)。 SampleRates_Str上的系统崩溃但在SampleRates_Str1上运行 - 但是当我在ADK中检查它们时,它们看起来具有完全相同的内容(至少对我而言)......
public void createListAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a Sampling Rate");
String[] SampleRates_Str1 = {"48000", "44100", "32000", "22050", "16000", "11025", "8000", "4000"};
builder.setSingleChoiceItems(SampleRates_Str1, 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();
alert.show();
}
为什么会有差异?
崩溃日志中的前几行:
10-11 04:56:36.746: E/AndroidRuntime(3144): FATAL EXCEPTION: main
10-11 04:56:36.746: E/AndroidRuntime(3144): Process: processing.test.soundanalyzer, PID: 3144
10-11 04:56:36.746: E/AndroidRuntime(3144): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
10-11 04:56:36.746: E/AndroidRuntime(3144): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
提前致谢, 问罗伯。
答案 0 :(得分:0)
array1只有8个变量。所以
final int SAMPLERATES_NUM = 9;
应该是
final int SAMPLERATES_NUM = 8;
或者至少有一个变量未初始化为保持为空。