1

我在谷歌上搜索,但没有得到积极的东西。我不知道为什么我在警报 dailog.create() 上收到空指针异常

我正在单击一个按钮创建一个 dailog,该按钮具有一个列表视图和一个线性布局中的按钮

            viewReport.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub


                    //created listMap
                    final String value[]=getKArrayFromMap(listMap);
                    final AlertDialog alertDialog = new AlertDialog.Builder(
                            getParent()).create();
                    LinearLayout linearLayout = new LinearLayout(getParent());
                    linearLayout.setOrientation(LinearLayout.VERTICAL);

                    Button ok=new Button(getParent());

                    final ListView lv = new ListView(getParent());
                    // simple_list_item_checked
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getParent(),
                            android.R.layout.simple_list_item_multiple_choice, value);
                    lv.setAdapter(adapter);
                    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                    linearLayout.addView(lv);
                    linearLayout.addView(ok);
                    alertDialog.setView(linearLayout);

                    ok.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            // some work
                    }



                            );
                    alertDialog.show();


                }
            });

在以下位置出现空指针异常:

09-27 11:37:37.033: E/AndroidRuntime(1997): FATAL EXCEPTION: main
09-27 11:37:37.033: E/AndroidRuntime(1997): java.lang.NullPointerException
09-27 11:37:37.033: E/AndroidRuntime(1997):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at com.cloudchowk.his.doctor.laboratory.BindDetailsActivity$BackGroundProcess$6.onClick(BindDetailsActivity.java:794)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at android.view.View.performClick(View.java:4084)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at android.view.View$PerformClick.run(View.java:16966)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at android.os.Handler.handleCallback(Handler.java:615)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at android.os.Looper.loop(Looper.java:137)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at java.lang.reflect.Method.invokeNative(Native Method)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at java.lang.reflect.Method.invoke(Method.java:511)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-27 11:37:37.033: E/AndroidRuntime(1997):     at dalvik.system.NativeStart.main(Native Method)

第 794 行:

final AlertDialog alertDialog = new AlertDialog.Builder(
                            getParent()).create();
4

3 回答 3

0

这样做可能会帮助你

viewReport.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                //created listMap
                final String value[]=getKArrayFromMap(listMap);
                final AlertDialog alertDialog = new AlertDialog.Builder(
                        MainActivity.this).create();
                LinearLayout linearLayout = new LinearLayout(MainActivity.this);
                linearLayout.setOrientation(LinearLayout.VERTICAL);

                Button ok=new Button(MainActivity.this);

                final ListView lv = new ListView(MainActivity.this);
                // simple_list_item_checked
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_list_item_multiple_choice, value);
                lv.setAdapter(adapter);
                lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                linearLayout.addView(lv);
                linearLayout.addView(ok);
                alertDialog.setView(linearLayout);

                ok.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        // some work
                }



                        );
                alertDialog.show();


            }
        });


    }
于 2013-09-27T11:34:40.223 回答
0

通过以下方式更改为:

final AlertDialog alertDialog = new AlertDialog.Builder(
                        yourActivity.this).create();

您必须在使用getParent()的地方更改YourActivity.this

于 2013-09-27T11:36:20.137 回答
0

我刚刚完成了这段代码,

okay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final AlertDialog alertDialog = new AlertDialog.Builder(
                    MainActivity.this).create();
            LinearLayout linearLayout = new LinearLayout(
                    getApplicationContext());
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            Button ok = new Button(getApplicationContext());
            final ListView lv = new ListView(getApplicationContext());
            lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    getApplicationContext(),
            android.R.layout.simple_list_item_multiple_choice,
                    value);
            lv.setAdapter(adapter);
            linearLayout.addView(lv);
            linearLayout.addView(ok);
            alertDialog.setView(linearLayout);
            ok.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),
                            "asdhfkja is done", Toast.LENGTH_LONG).show();
                }
            });
            alertDialog.show();
        }
    });

或者只是改变

final AlertDialog alertDialog = new AlertDialog.Builder(
                    YOUR_ACTIVITY.this).create();

并将所有替换getParent()getApplicationContect()

于 2013-09-27T12:34:06.310 回答