如何在 onResponse 方法中创建一个对话框?
How to create a dialog inside onResponse method?
这是我的对话代码
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
?
注意:其非activity class
然后在 Application class 中为上下文创建 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));
从 activity class 开始,当您初始化 class 时,像这样传递上下文:
new My_Non_ActivityClass(MainActivity.this);
现在在您的 class 中创建这样的构造函数并获取上下文:
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();
}
或者无需传递上下文,只需访问它,因为它是您 class 中的全局变量。
注意:如果您在服务中,该服务有其自己的上下文。只需使用 this
.
这是我的对话代码
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
?
注意:其非activity class
然后在 Application class 中为上下文创建 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));
从 activity class 开始,当您初始化 class 时,像这样传递上下文:
new My_Non_ActivityClass(MainActivity.this);
现在在您的 class 中创建这样的构造函数并获取上下文:
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();
}
或者无需传递上下文,只需访问它,因为它是您 class 中的全局变量。
注意:如果您在服务中,该服务有其自己的上下文。只需使用 this
.