【问题标题】:How to create a dialog inside onResponse method? [duplicate]如何在 onResponse 方法中创建对话框? [复制]
【发布时间】:2017-05-09 10:41:22
【问题描述】:

这是我的对话代码

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

我想在下面的 onResponse 方法中使用它

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 中断应用程序并在控制台中显示以下错误

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

我应该用什么替换context

注意:它的非活动类

【问题讨论】:

  • 如果在活动类中则使用ActivityName.this,如果是非活动类则使用getApplicationContext();
  • “onResponse”方法是什么?
  • @Piyush 其非活动类并使用 getApplicationContext() 在控制台上以 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 错误破坏应用程序
  • 显示该类
  • onResponse 是在活动中还是在片段中还是在服务中?

标签: java android retrofit2 okhttp3


【解决方案1】:

从活动类中,当你初始化你的类时,像这样传递上下文:

new My_Non_ActivityClass(MainActivity.this);

现在在你的类中创建一个这样的构造函数并获取上下文:

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

现在你有了上下文,像这样使用任何你想要的地方:

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

或者不需要传递上下文,只需访问它,因为它是类中的全局变量。

注意:如果您在服务中,则该服务有自己的上下文。只需使用this

【讨论】:

  • Service是一个Context,所以Context context = this;在构造函数中执行它并传递给该方法。
【解决方案2】:

然后在 Application 类中为上下文创建 getter setter 方法 通过这种用法可以跨应用程序获取上下文

改变这一行

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

有了这个

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

【讨论】:

  • 你的意思是修改Application类?我怎样才能访问它?我是安卓新手
猜你喜欢
  • 1970-01-01
  • 2016-05-31
  • 2013-04-11
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
相关资源
最近更新 更多