【发布时间】: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