-1

Here is my dialog code

public void registrationSuccess(final Context context, String warning, String message) {
        alert = new AlertDialog.Builder(context);
        alert.setTitle(warning);
        alert.setMessage(message)
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Intent i = new Intent(context, loginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        context.startActivity(i);
                    }
                });
        AlertDialog alertDialog = alert.create();
        alert.show();
    }

and I want to use it inside my onResponse method below

client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            System.out.println("Failure!!");
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if(response.isSuccessful()) {
                registrationSuccess(getApplicationContext(), getResources().getString(R.string.congrats), getResources().getString(R.string.successfull_registration));
            } else {
                System.out.println("FAILED");

            }
        }

    });

getApplicationContext breaks the app with the following error showing in the console

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

With what should I replace context?

NOTE: its non-activity class

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Behrouz Riahi
  • 1,751
  • 2
  • 21
  • 42

2 Answers2

3

From activity class, when you initialize your class, pass the context like this:

new My_Non_ActivityClass(MainActivity.this);

now in your class create a constructor like this and get context:

Context context;
My_Non_ActivityClass(Context c){
     context = c;
}

Now you have context, use wherever you want like this:

public void registrationSuccess(context, String warning, String message) {
    alert = new AlertDialog.Builder(context);
    alert.setTitle(warning);
    alert.setMessage(message)
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent i = new Intent(context, loginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(i);
                }
            });
    AlertDialog alertDialog = alert.create();
    alert.show();
}

Or no need to pass context, just access it as it is global variable in your class.

NOTE: If you are in service, the service has its own context. Just use this.

Kaushal28
  • 5,377
  • 5
  • 41
  • 72
0

Create getter setter methods in Application class for context then with this use can get context across the application

change this line

registrationSuccess(null, getResources().getString(R.string.congrats), getResources().getString(R.string.successfull_registration));

With this

registrationSuccess(ApplicationControler.getContext(), getResources().getString(R.string.congrats), getResources().getString(R.string.successfull_registration));
Bharat singh
  • 504
  • 7
  • 17