How to use Toast in ASyncTask/onPostExecute with string from resources?
13,439
Change getBaseContext()... to getApplicationContext().....
Toast.makeText(getApplicationContext(), getApplicationContext().getResources().getString(R.string.toast_sync_completed), Toast.LENGTH_SHORT).show();
Author by
LA_
To search in your posts only type: user:me text To list all your unaccepted questions, type: user:me hasaccepted:0 My questions list: link
Updated on June 04, 2022Comments
-
LA_ almost 2 years
I have the following code:
@Override protected void onPostExecute(Void arg0) { ... Toast.makeText(getBaseContext(), getBaseContext().getResources().getString(R.string.toast_sync_completed), Toast.LENGTH_SHORT).show(); }
It fails (FC). If pass
"Test string"
instead ofgetResources().getString(R.string.toast_sync_completed)
, then it works correctly. What am I doing wrong?-
Kartik Domadiya about 13 yearsTry this code : Toast.makeText(getApplicationContext(), getApplicationContext().getResources().getString(R.string.toast_sync_completed), Toast.LENGTH_SHORT).show();
-
-
LA_ about 13 yearsActually it works both with ApplicationContext and BaseContext - the problem was in another code.
-
user1911703 about 9 yearsprotected void onPostExecute(String result) { Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); } For me it's not working. Error shows: The method getApplicationContext() is undefined for the type SingupMember