0

I preferred to assemble the code, that creates an alert into a method and wraps it into a separate class.

But, unfortunately, after an event is called, the application crashes, I think it's because of the context that is passed in the method. Please tell me how to correctly process the context parameter in a method wrapped in a class.

This is a method in separate class:

 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();
        }

This is a method invocation in 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;
        }
    });
}

How can I call it in the MainActivity?

Here is the error log:

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)
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Artem Fedotov
  • 432
  • 1
  • 6
  • 21
  • 3
    If it crashes, include the stack trace and line number in the question. – Nabin Bhandari Oct 25 '17 at 09:17
  • 2
    share crash log – J Ramesh Oct 25 '17 at 09:17
  • I copied the logs in the gist https://gist.github.com/fedotarte/e0d1f8e68e8e3e5998735c185a2725c0 – Artem Fedotov Oct 25 '17 at 09:22
  • 2
    `alertBuiders` is null. You forgot to initialize it properly. There's really no need to make that method an instance member. Make it `static`, and call it directly on the class, instead of creating an instance first. Also, in the future, you need to include the stack trace in the question. Please don't link to it off-site. – Mike M. Oct 25 '17 at 09:23
  • @MikeM. Tanks for your help, after initializing an instance of the class everything works correctly. – Artem Fedotov Oct 25 '17 at 09:30

1 Answers1

1

Try this

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

// write way is to create a dialog is AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context).create();

Mohit Kalia
  • 351
  • 1
  • 6