当我在开始另一项活动之前不打电话给Toast时,事情就会按预期进行。但是,如果我在startActivity之前调用Toast,则该活动将不会开始。请检查以下代码以了解我的问题:
class LoginTask extends HttpAsyncTask {
@Override
protected void onPostExecute(String result) {
if (result != null) {
LoginResponse loginResponse = (LoginResponse) getMappedModel(result, LoginResponse.class);
if(loginResponse.getResult().equals("success")) {
/*startActivity works only if I comment this line*/ Toast.makeText(getBaseContext(), "Logged in Successfully!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getBaseContext(), "Wrong username or password!", Toast.LENGTH_LONG).show();
}
}
}
}
最佳答案
在没有看到所有代码的情况下,我建议使用getApplicationContext,这将返回应用程序的上下文。
Toast.makeText(getApplicationContext (), "Logged in Successfully!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext (), MainActivity.class);