AlertBuilder方法调用中的Context参数

时间:2017-10-25 09:14:19

标签: java android alertdialog

我更喜欢组装代码,它会在方法中创建警报并将其包装到单独的类中。

但是,不幸的是,在调用一个事件后,应用程序崩溃了,我认为这是因为该方法中传递的上下文。请告诉我如何正确处理包含在类中的方法中的context参数。

这是一个单独的类中的方法:

 public void showAlertDialogWithTwoButtons(Context context, CharSequence title, CharSequence message, final DialogCallback callback) {
            this.context=context;
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

            // set dialog message
            alertDialogBuilder
                    .setTitle(title.toString())
                    .setMessage(message.toString())
                    .setCancelable(false)
                    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            if (callback != null) {
                                callback.onClickYes();
                            }
                            dialog.cancel();
                        }
                    })
                    .setNegativeButton("No",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            if (callback != null) {
                                callback.onClickNo();
                            }
                            dialog.cancel();
                        }
                    });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }

这是MainActivity中的方法调用:

if (id == R.id.action_hide) {
    alertBuiders.showAlertDialogWithTwoButtons(MainActivity.this, "Wanna hide?", "Do you want to hide?", new DialogCallback() {
        @Override
        public void onClickYes() {
            hideApp();
        }
        @Override
        public void onClickNo() {
            return;
        }
    });
}

如何在MainActivity中调用它?

以下是错误日志:

10-25 12:19:59.361 4929-4929/com.example.fedotarte.myfirstapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        Process: com.example.fedotarte.myfirstapplication, PID: 4929
                                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.fedotarte.myfirstapplication.AlertBuiders.showAlertDialogWithTwoButtons(android.content.Context, java.lang.CharSequence, java.lang.CharSequence, com.example.fedotarte.myfirstapplication.DialogCallback)' on a null object reference
                                                                                            at com.example.fedotarte.myfirstapplication.MainActivity.onOptionsItemSelected(MainActivity.java:79)
                                                                                            at android.app.Activity.onMenuItemSelected(Activity.java:3435)
                                                                                            at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:360)
                                                                                            at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:194)
                                                                                            at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:110)
                                                                                            at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:110)
                                                                                            at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
                                                                                            at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:204)
                                                                                            at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:776)
                                                                                            at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:821)
                                                                                            at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
                                                                                            at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:968)
                                                                                            at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:127)
                                                                                            at android.widget.AdapterView.performItemClick(AdapterView.java:318)
                                                                                            at android.widget.AbsListView.performItemClick(AbsListView.java:1165)
                                                                                            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3134)
                                                                                            at android.widget.AbsListView$3.run(AbsListView.java:4049)
                                                                                            at android.os.Handler.handleCallback(Handler.java:789)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                            at android.os.Looper.loop(Looper.java:164)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

1 个答案:

答案 0 :(得分:1)

试试这个

        // create alert dialog is wrong way remove this code
        AlertDialog alertDialog = alertDialogBuilder.create();

//写方式是创建一个对话框 AlertDialog.Builder alertDialogBu​​ilder = new AlertDialog.Builder(context).create();