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();
Share:
13,439
LA_
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, 2022

Comments

  • LA_
    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 of getResources().getString(R.string.toast_sync_completed), then it works correctly. What am I doing wrong?

    • Kartik Domadiya
      Kartik Domadiya about 13 years
      Try this code : Toast.makeText(getApplicationContext(), getApplicationContext().getResources().getString(R.string.to‌​ast_sync_completed), Toast.LENGTH_SHORT).show();
  • LA_
    LA_ about 13 years
    Actually it works both with ApplicationContext and BaseContext - the problem was in another code.
  • user1911703
    user1911703 about 9 years
    protected 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