這裏是我的對話框代碼如何在onResponse方法中創建一個對話框?
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類中,那麼使用'ActivityName.this',如果它是非activity類,那麼使用'getApplicationContext();' – Piyush
「onResponse」方法是什麼? –
@Piyush它的非活動類,並使用getApplicationContext()打破了與應用程序'java.lang.NullPointerException:試圖調用虛擬方法'android.content.Context android.content.Context.getApplicationContext()'空對象引用'控制檯上的錯誤 –