0

我有一个列表视图,当我单击一个项目时,它应该显示一个带有单选按钮的 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();
                }
            }
        });
    }
4

1 回答 1

2

levelDialog在实例化之前调用一个函数

     levelDialog.dismiss();
     levelDialog = builder.create();
     levelDialog.show();

将其更改为

     levelDialog = builder.create();
     levelDialog.show();
     levelDialog.dismiss();

但我不知道你为什么打电话给dismiss()那里。如果这不能解决问题,请发布 logcat 以便我们知道它在哪里NPE。更清楚地说,切换这些行应该可以解决您的问题NPE,但dismiss()不应该在那里被调用。

编辑

拿出这个

AlertDialog levelDialog=null;

并像这样在这里声明和初始化它

       final AlertDialog levelDialog = builder.create();
       levelDialog.show();

并调用dismiss()你的onClick()但使用dialog.dismiss()而不是levelDialog.dismiss()

此外,它通常更好地使用Activity Context而不是Application ContextIntent可能会更改getApplicationContext()MyActivity.this

于 2013-05-29T18:30:26.643 回答