1

Seems like the displayView variable is not compatible with void, i try to add a progress dialog into a navigation drawer, every time that an element its selected

public class AsyncClass extends AsyncTask<Void, String, Void> {
     private Context context;
     ProgressDialog dialog = new ProgressDialog(context);

     public AsyncClass(Context cxt) {
         context = cxt;
     }
     @Override
     protected void onPreExecute() {
         dialog.setTitle("Please wait");
         dialog.show();
     }
     @Override
     protected Void doInBackground(Void... unused) {
      
      public void displayView(int position) { /*the message here is :void is an invalid type for the variable displayView*/

       
       
       Fragment fragment = null;
       switch (position) { 
       
       case 1:
        fragment = new  HomeFragment();
        break;
       case 2:
        fragment = new  CalendarioFragment();
        break;
       case 5:
        fragment = new  ContactoFragment();
        break;
       case 6:
        fragment = new  OnclickFragment();
        break;
       default:
        break;
       }

       if (fragment != null) {
        try{
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

        // update selected item and tit|le, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        }
        catch(Exception E)
        {
         Log.e("MainActivity", "Error in creating fragment");
        }
       } 
       else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
       }
      }
      SystemClock.sleep(2000);
         return (null);
     }

     @Override
     protected void onPostExecute(Void unused) {
         dialog.dismiss();
     }
 }
it says "void is an invalid type for the variable displayView"
Pamme Cobos
  • 319
  • 1
  • 3
  • 13

1 Answers1

0

First of all Java doesn't support nested methods, which you are doing in doInBackground(). This is the reason for given error. Check here in details.

Another thing is that, doInBackground() will be running on background thread so don't update UI from there. AsyncTask is used only for long running operations. You can consider moving you doInBackground() code to relevant Activity or Fragment.

Community
  • 1
  • 1
Uniruddh
  • 4,427
  • 3
  • 52
  • 86