リストビューがあり、アイテムをクリックすると、ラジオボタンを含む AlertDialog が表示されます。しかし、alertDialog を呼び出すと、アプリがクラッシュします。Logcat は NullPointerException を返します。私は何を間違っていますか?
private void registerClickCallBack() {
ListView list = (ListView) findViewById(R.id.settingsList);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
if (position == 0) {
final AlertDialog levelDialog
final CharSequence[] items = { " Easy ", " Medium ",
" Hard ", " Very Hard " };
AlertDialog.Builder builder = new AlertDialog.Builder(
MyActivity.this);
builder.setTitle("Select The Difficulty Level");
builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int item) {
Intent i = new Intent(
getApplictionContext(),
MyActivity.class);
switch (item) {
case 0:
String msg = "Alert A";
Toast.makeText(
MyActivity.this,
msg, Toast.LENGTH_LONG).show();
break;
case 1:
String msga = "Alert B";
Toast.makeText(
MyActivity.this,
msga, Toast.LENGTH_LONG).show();
break;
case 2:
String msgz = "Alert C";
Toast.makeText(
MyActivity.this,
msgz, Toast.LENGTH_LONG).show();
break;
case 3:
String msge = "Alert E";
Toast.makeText(
MyActivity.this,
msge, Toast.LENGTH_LONG).show();
break;
}
}
});
levelDialog.dismiss();
levelDialog = builder.create();
levelDialog.show();
}
else if (position == 1) {
String msg = "1";
Toast.makeText(MyActivity.this, msg,
Toast.LENGTH_LONG).show();
} else if (position == 2) {
String msg = "2";
Toast.makeText(MyActivity.this, msg,
Toast.LENGTH_LONG).show();
}
else if (position == 3) {
String msg = "3";
Toast.makeText(MyActivity.this, msg,
Toast.LENGTH_LONG).show();
}
}
});
}