单选项上的AlertDialog错误

时间:2014-05-01 04:06:25

标签: android

我是android developpement的新手我创建了一个新的活动,显示了一个单选项,但是在编译时我生成了一个错误。

首先我有对话框显示但没有显示单选项目,只显示了构建器的标题和消息,然后经过几次尝试,没有显示,它关闭了程序。

这是我的logcat:

05-01 04:59:20.078: E/AndroidRuntime(19456): FATAL EXCEPTION: main
05-01 04:59:20.078: E/AndroidRuntime(19456): java.lang.NullPointerException
05-01 04:59:20.078: E/AndroidRuntime(19456):    at com.androidtest2.Dialogs.showSecondDialog(Dialogs.java:98)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at com.androidtest2.Dialogs.access$1(Dialogs.java:78)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at com.androidtest2.Dialogs$2.onClick(Dialogs.java:38)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at android.view.View.performClick(View.java:4162)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at android.view.View$PerformClick.run(View.java:17082)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at android.os.Handler.handleCallback(Handler.java:615)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at android.os.Looper.loop(Looper.java:137)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at android.app.ActivityThread.main(ActivityThread.java:4867)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at java.lang.reflect.Method.invokeNative(Native Method)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at java.lang.reflect.Method.invoke(Method.java:511)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
05-01 04:59:20.078: E/AndroidRuntime(19456):    at dalvik.system.NativeStart.main(Native Method)

这是我的活动:

 public class Dialogs extends Activity {

        private AlertDialog.Builder dialogBuilder;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setContentView(R.layout.activity_dialogs);

            Button d1 = (Button) findViewById(R.id.dialog1);
            Button d2 = (Button) findViewById(R.id.dialog2);

            d1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    showFirstDialog();

                }
            });

            d2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    showSecondDialog();

                }
            });

        }

        private void showSecondDialog(){
                    // variables
                    AlertDialog.Builder ddialogBuilder = new AlertDialog.Builder(getApplicationContext());
                    final String[] classArray = {"Mage","Paladin","Guerrier","Druide","Chaman"};

                    // process
                    ddialogBuilder.setTitle("Character's Class");
                    ddialogBuilder.setMessage("Choose your Class");
                    ddialogBuilder.setSingleChoiceItems(classArray, -1, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getApplicationContext(), classArray[which], Toast.LENGTH_LONG).show();

                        }
                    });



                    // output
                    AlertDialog dialog = dialogBuilder.create();
                    dialog.show();
        }

    }

2 个答案:

答案 0 :(得分:3)

请参阅您的实例变量名称

  

AlertDialog.Builder ddialogBu​​ilder = new AlertDialog.Builder(getApplicationContext());

但你用过

  

AlertDialog dialog = dialogBu​​ilder.create();

这里的实例变量,即dialogBuilder不匹配。 这就是你得到空指针异常的原因。所以在这里你需要将该行改为

  
    

AlertDialog dialog = ddialogBu​​ilder.create();

  

答案 1 :(得分:0)

您可以面对的另一个问题是无法添加窗口 - 令牌null不适用于以getApplication()作为上下文的应用程序

为避免此错误,您应该声明ddialogBuilder,如下所示。

AlertDialog.Builder ddialogBuilder = new AlertDialog.Builder(Dialogs.this);