Search code examples
androidandroid-fragmentsandroid-recyclerviewandroid-asynctaskandroid-viewpager

Asynctask crash ViewPager RecyclerView (Android)


Currently, I am working on an android project, where I have implemented a bottom navigation view and a swiping effect, where I can swap fragments left and right. They are synchronized.

Unfortunately, I have 2 errors, which I can't solve.

  1. Asynctask crashes when i swipe. I receive following error:

E/MessageQueue-JNI: java.lang.IllegalStateException: Cannot execute task: the task is already running.

Which is because the Asynctask is called, when it's already running?

I imagine that I have to cancel the Asynctask before running it, but how do i do that?

  1. I have 5 fragments to start with, which you can see on my bottom navigation view. They also change by swiping left and right on the screen. On my second item on navigation bar, I have a Cardview, which is implemented with recyclerViewAdapter. Whenever I click on one of the cardviews, then it's supposed to change to a new fragment. A fragment not showing in the navigation view. Inside the recyclerViewAdapter Java class there's an onClick, which should change the fragments, whenever I click on one of the cardviews, but for some reason, it doesn't work.

Image is here:

enter image description here

* CODE HERE *

MainActivity:

@Override
public void onBackPressed() {
    if (mPager.getCurrentItem() == 0) {
        // If the user is currently looking at the first step, allow the system to handle the
        // Back button. This calls finish() on this activity and pops the back stack.
        super.onBackPressed();
    } else {
        // Otherwise, select the previous step.
        mPager.setCurrentItem(mPager.getCurrentItem() - 1);
    }
}

/**
 * view pager.
 */

public void setupViewPager(ViewPager viewPager){

    ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager());
    adapter.addFragment(new StartsideFragment());
    adapter.addFragment(new CardViewTabelFragment());
    adapter.addFragment(new SensorOversigtFragment());
    adapter.addFragment(new KontaktFragment());
    viewPager.setAdapter(adapter);

}

public void setViewPager(int fragmentNumber){

    mPager.setCurrentItem(fragmentNumber);

}

public void enableViewPagerSwitch(){

    mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            if (prevMenuItem != null) {
                prevMenuItem.setChecked(false);
            }
            else
            {
                mBottomNav.getMenu().getItem(0).setChecked(false);
            }
            Log.d("page", "onPageSelected: "+position);
            mBottomNav.getMenu().getItem(position).setChecked(true);
            prevMenuItem = mBottomNav.getMenu().getItem(position);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    setupViewPager( mPager );

}



public void bot_Navigation() {

    mBottomNav = (BottomNavigationView) findViewById( R.id.nav_bot );

    botNavHelper.disableShiftMode( mBottomNav );

    mBottomNav.setOnNavigationItemSelectedListener( new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {

                case R.id.bot_startside:
                    // overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_left );
                    //mPager.setCurrentItem( 0 );
                    setViewPager( 0 );
                    //fragment = new StartsideFragment();

                    break;

                case R.id.bot_datatabel:
                    //overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_left );
                    //mPager.setCurrentItem( 1 );
                    setViewPager( 1 );
                    //fragment = new CardViewTabelFragment();
                    //viewPager.setCurrentItem( 1 );
                    break;

                case R.id.bot_sensorOversigt:
                    //overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_left );
                    //mPager.setCurrentItem( 2 );
                    setViewPager( 2 );
                    //fragment = new KontaktFragment();
                    break;

                case R.id.bot_kontakt:
                    //overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_left );
                    //mPager.setCurrentItem( 3 );
                    setViewPager( 3 );
                    //fragment = new SensorOversigtFragment();
                    break;

                case R.id.bot_logUd:
                    //overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_left );
                    //mPager.setCurrentItem( 4 );
                    setViewPager( 4 );
                    alertDialog();
                    break;
            }

            if (fragment != null) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace( R.id.flMain, fragment );
                ft.commit();
            }

            return true;
        }
    } );

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );

    onCreate2();

    bot_Navigation();


}

protected void onCreate2() {
    //  protected void onCreate2(Bundle savedInstanceState) {
    //  super.onCreate(savedInstanceState);
    setContentView( R.layout.activity_main );
    Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar );
    setSupportActionBar( toolbar );
    //toolbar.setLogo( R.mipmap.ic_launcher_cxweb_black );

    TextView mTitle = (TextView) toolbar.findViewById( R.id.toolbartitle );

    mTitle.setText( toolbar.getTitle() );

    getSupportActionBar().setDisplayShowTitleEnabled( false );

    //toolbar.setTitle( "LeoSenses" );

    mPager = (ViewPager) findViewById( R.id.flMain );

    enableViewPagerSwitch();

    DrawerLayout drawer = (DrawerLayout) findViewById( R.id.drawer_layout );
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close );
    drawer.addDrawerListener( toggle );
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById( R.id.nav_view );
    navigationView.setNavigationItemSelectedListener( this );


    //Default fragment for startside

    android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace( R.id.flMain, new StartsideFragment() );
    ft.commit();

    navigationView.setCheckedItem( R.id.nav_startside );

    PreferenceManager.getDefaultSharedPreferences( this ).getBoolean( "brugPosVedStart", true );

}

ViewPageAdapter:

public class ViewPageAdapter extends FragmentStatePagerAdapter {

private final List<Fragment> mFragmentList = new ArrayList<>();

public ViewPageAdapter(FragmentManager fm) {
    super( fm );
}

public void addFragment(Fragment fragment) {
    mFragmentList.add( fragment );
}

@Override
public Fragment getItem(int position) {
    return mFragmentList.get( position );
}

@Override
public int getCount() {
       return mFragmentList.size();
    }

}

RecyclerViewAdapter:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {

private ViewPager mPager;

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from( viewGroup.getContext() ).inflate( R.layout.fragment_card_view_tabel, viewGroup, false );

    viewGroup.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View view) {

        }
    } );

    return new RecyclerViewHolder( view );
}

@Override
public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int i) {

    recyclerViewHolder.mBeskrivelse.setText( OurData.beskrivelse[i] );
    recyclerViewHolder.mTitle.setText( OurData.title[i] );
    recyclerViewHolder.cardView.setCardBackgroundColor( Color.parseColor( OurData.colors[i] ) );

}

@Override
public int getItemCount() {
    return OurData.title.length;
}


class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public int nummer;
    private TextView mBeskrivelse;
    private TextView mTitle;
    private CardView cardView;


    public RecyclerViewHolder(View itemView) {
        super( itemView );
        mPager = (ViewPager) itemView.findViewById( R.id.flMain );
        mBeskrivelse = (TextView) itemView.findViewById( R.id.item_beskrivelse );
        mTitle = (TextView) itemView.findViewById( R.id.item_title );
        cardView = (CardView) itemView.findViewById( R.id.card_view );
        itemView.setOnClickListener( this );

    }


    @Override
    public void onClick(View v) {

        nummer = getAdapterPosition();

        if (nummer == 0) {
            //mPager.setCurrentItem(0);

            ((FragmentActivity) itemView.getContext()).getSupportFragmentManager().beginTransaction().replace( R.id.flMain, new DataTabelFragment() ).commit();

        } else if (nummer == 1) {

            ((FragmentActivity) itemView.getContext()).getSupportFragmentManager().beginTransaction().replace( R.id.flMain, new Sensor1Fragment() ).commit();

        } else if (nummer == 2) {

            ((FragmentActivity) itemView.getContext()).getSupportFragmentManager().beginTransaction().replace( R.id.flMain, new Sensor2Fragment() ).commit();

        } else if (nummer == 3) {

            ((FragmentActivity) itemView.getContext()).getSupportFragmentManager().beginTransaction().replace( R.id.flMain, new Sensor3Fragment() ).commit();

        }
       }
    }
}

One of the fragments with an asynctask:

public class DataTabelFragment extends Fragment implements jsonAsynctask.DataTabelFragment {

public TextView sensor1;

jsonAsynctask jsonasynctask = new jsonAsynctask( this );


public DataTabelFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate( R.layout.fragment_data_tabel, container, false );

    sensor1 = (TextView) view.findViewById( R.id.sensorAlleBox );

    jsonasynctask.execute();

    return view;

}

@Override
public void onJobFinishListener(List<String> allId) {
    //when this method is trigered by your asynctask
    //it means that you are in ui thread and update your ui component

    for (int i = 0; i < allId.size(); i++) {

        sensor1.append( jsonasynctask.allId.get( i ) + " | " + jsonasynctask.allDevice.get( i ) + " | " + jsonasynctask.allTemp.get( i ) + " | " + jsonasynctask.allHum.get( i ) + " | " + jsonasynctask.allBat.get( i ) + " | " + jsonasynctask.allMode.get( i ) + " | " + jsonasynctask.allLux.get( i ) + " | " + jsonasynctask.allDate_time.get( i ) + "\n\n" );

    }
 }

}

* UPDATE *

FRAGMENT WHERE ERROR OCCURS WITH ASYNCTASK:

public class SensorOversigtFragment extends Fragment implements jsonAsynctask.DataTabelFragment {

private TextView firstValue;
private TextView firstValue2;
private TextView firstDevice;
private TextView secondValue;
private TextView secondDevice;
private TextView secondValue2;

private ImageView firstImage, secondImage;

private View firstView, secondView;

private LinearLayout linear1, linear2;

jsonAsynctask jsonasynctask = new jsonAsynctask( this );

public SensorOversigtFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate( R.layout.content_sensor_oversigt, container, false );

    firstValue = (TextView) view.findViewById( R.id.firstValue );
    firstDevice = (TextView) view.findViewById( R.id.firstDevice );
    firstValue2 = (TextView) view.findViewById( R.id.firstValue2 );
    secondDevice = (TextView) view.findViewById( R.id.secondDevice );
    secondValue = (TextView) view.findViewById( R.id.secondValue );
    secondValue2 = (TextView) view.findViewById( R.id.secondValue2 );

    linear1 = (LinearLayout) view.findViewById( R.id.linear1 );
    linear2 = (LinearLayout) view.findViewById( R.id.linear2 );

    firstImage = (ImageView) view.findViewById( R.id.firstImage );
    secondImage = (ImageView) view.findViewById( R.id.secondImage );

    firstView = (View) view.findViewById( R.id.firstView );
    secondView = (View) view.findViewById( R.id.secondView );

    if (jsonasynctask.getStatus() == AsyncTask.Status.RUNNING) {
        jsonasynctask.cancel( true );
    } else if (jsonasynctask.getStatus() != AsyncTask.Status.RUNNING) {
        jsonasynctask.execute();
    }

    return view;
}

@Override
public void onJobFinishListener(List<String> allId) {

    //System.out.println( "LAST MODE: " + jsonasynctask.allMode.get( allId.size() - 1 ) );

    for (int i = 0; i < allId.size(); i++) {

        if (jsonasynctask.allDevice.get( i ).equals( "B42DB2" )) {

            if (jsonasynctask.allMode.get( i ).equals( "0" )) {

                linear1.setBackgroundColor( Color.parseColor( "#A9A9A9" ) );
                firstDevice.setBackgroundColor( Color.parseColor( "#808080" ) );

                firstDevice.setText( "STANDBY MODE" );

                firstValue.setVisibility( View.GONE );
                firstValue2.setVisibility( View.GONE );

                firstImage.setColorFilter( Color.parseColor( "#A9A9A9" ) );
                firstView.setBackgroundColor( Color.parseColor( "#A9A9A9" ) );

            } else if (jsonasynctask.allMode.get( i ).equals( "1" )) {

                linear1.setBackgroundColor( Color.parseColor( "#A0D468" ) );
                firstValue.setBackgroundColor( Color.parseColor( "#8CC152" ) );
                firstValue2.setBackgroundColor( Color.parseColor( "#8CC152" ) );
                firstDevice.setBackgroundColor( Color.parseColor( "#8CC152" ) );
                firstView.setBackgroundColor( Color.parseColor( "#A0D468" ) );

                firstImage.setImageResource( R.drawable.lighticon );

                firstValue.setText( jsonasynctask.allTemp.get( i ) + "°C" );
                firstValue2.setText( jsonasynctask.allHum.get( i ) + "%" );
                firstDevice.setText( jsonasynctask.allDevice.get( i ) );

                firstValue.setVisibility( View.VISIBLE );
                firstValue2.setVisibility( View.VISIBLE );

                firstImage.setColorFilter( Color.parseColor( "#FFFFFF" ) );

            } else if (jsonasynctask.allMode.get( i ).equals( "2" )) {

                linear1.setBackgroundColor( Color.parseColor( "#FFCE54" ) );
                firstValue.setBackgroundColor( Color.parseColor( "#F68B42" ) );
                firstValue2.setBackgroundColor( Color.parseColor( "#F68B42" ) );
                firstDevice.setBackgroundColor( Color.parseColor( "#F68B42" ) );
                firstView.setBackgroundColor( Color.parseColor( "#FFCE54" ) );

                firstImage.setImageResource( R.drawable.light_bulb_7_512 );

                firstValue.setText( jsonasynctask.allLux.get( i ) );
                firstValue2.setText( "Lux" );
                firstDevice.setText( jsonasynctask.allDevice.get( i ) );

                firstValue.setVisibility( View.VISIBLE );
                firstValue2.setVisibility( View.VISIBLE );

                firstImage.setColorFilter( Color.parseColor( "#FFFFFF" ) );

            } else if (jsonasynctask.allMode.get( i ).equals( "3" )) {

                linear1.setBackgroundColor( Color.parseColor( "#4FC1E9" ) );
                firstValue.setBackgroundColor( Color.parseColor( "#3BAFDA" ) );
                firstValue2.setBackgroundColor( Color.parseColor( "#3BAFDA" ) );
                firstDevice.setBackgroundColor( Color.parseColor( "#3BAFDA" ) );
                firstView.setBackgroundColor( Color.parseColor( "#4FC1E9" ) );

                firstImage.setImageResource( R.drawable.ic_baseline_meeting_room_24px );

                firstValue.setText( jsonasynctask.allDoor.get( i ) );
                firstValue2.setText( "Door" );
                firstDevice.setText( jsonasynctask.allDevice.get( i ) );

                firstValue.setVisibility( View.VISIBLE );
                firstValue2.setVisibility( View.VISIBLE );

                firstImage.setColorFilter( Color.parseColor( "#FFFFFF" ) );

            } else if (jsonasynctask.allMode.get( i ).equals( "4" )) {

                linear1.setBackgroundColor( Color.parseColor( "#509CEC" ) );
                firstValue.setBackgroundColor( Color.parseColor( "#4A89DC" ) );
                firstValue2.setBackgroundColor( Color.parseColor( "#4A89DC" ) );
                firstDevice.setBackgroundColor( Color.parseColor( "#4A89DC" ) );
                firstView.setBackgroundColor( Color.parseColor( "#509CEC" ) );

                firstImage.setImageResource( R.drawable.vibration );

                firstValue.setText( jsonasynctask.allVib.get( i ) );
                firstValue2.setText( "Vib" );
                firstDevice.setText( jsonasynctask.allDevice.get( i ) );

                firstValue.setVisibility( View.VISIBLE );
                firstValue2.setVisibility( View.VISIBLE );

                firstImage.setColorFilter( Color.parseColor( "#FFFFFF" ) );

            } else if (jsonasynctask.allMode.get( i ).equals( "5" )) {

                linear1.setBackgroundColor( Color.parseColor( "#EC87C0" ) );
                firstValue.setBackgroundColor( Color.parseColor( "#D770AD" ) );
                firstValue2.setBackgroundColor( Color.parseColor( "#D770AD" ) );
                firstDevice.setBackgroundColor( Color.parseColor( "#D770AD" ) );
                firstView.setBackgroundColor( Color.parseColor( "#EC87C0" ) );

                firstImage.setImageResource( R.drawable.ic_magnet );

                firstValue.setText( jsonasynctask.allMag.get( i ) );
                firstValue2.setText( "Mag" );
                firstDevice.setText( jsonasynctask.allDevice.get( i ) );

                firstValue.setVisibility( View.VISIBLE );
                firstValue2.setVisibility( View.VISIBLE );

                firstImage.setColorFilter( Color.parseColor( "#FFFFFF" ) );

            }

        } else if (jsonasynctask.allDevice.get( i ).equals( "B42DC6" )) {

            if (jsonasynctask.allMode.get( i ).equals( "0" )) {

                linear2.setBackgroundColor( Color.parseColor( "#A9A9A9" ) );
                secondDevice.setBackgroundColor( Color.parseColor( "#808080" ) );

                secondDevice.setText( "STANDBY MODE" );

                secondValue.setVisibility( View.GONE );
                secondValue2.setVisibility( View.GONE );

                secondImage.setColorFilter( Color.parseColor( "#A9A9A9" ) );
                secondView.setBackgroundColor( Color.parseColor( "#A9A9A9" ) );


            } else if (jsonasynctask.allMode.get( i ).equals( "1" )) {

                linear2.setBackgroundColor( Color.parseColor( "#A0D468" ) );
                secondValue.setBackgroundColor( Color.parseColor( "#8CC152" ) );
                secondValue2.setBackgroundColor( Color.parseColor( "#8CC152" ) );
                secondDevice.setBackgroundColor( Color.parseColor( "#8CC152" ) );
                secondView.setBackgroundColor( Color.parseColor( "#A0D468" ) );

                secondImage.setImageResource( R.drawable.lighticon );

                secondValue.setText( jsonasynctask.allTemp.get( i ) + "°C" );
                secondValue2.setText( jsonasynctask.allHum.get( i ) + "%" );
                secondDevice.setText( jsonasynctask.allDevice.get( i ) );

                secondValue.setVisibility( View.VISIBLE );
                secondValue2.setVisibility( View.VISIBLE );

                secondImage.setColorFilter( Color.parseColor( "#FFFFFF" ) );

            } else if (jsonasynctask.allMode.get( i ).equals( "2" )) {

                linear2.setBackgroundColor( Color.parseColor( "#FFCE54" ) );
                secondValue.setBackgroundColor( Color.parseColor( "#F68B42" ) );
                secondValue2.setBackgroundColor( Color.parseColor( "#F68B42" ) );
                secondDevice.setBackgroundColor( Color.parseColor( "#F68B42" ) );
                secondView.setBackgroundColor( Color.parseColor( "#FFCE54" ) );

                secondImage.setImageResource( R.drawable.light_bulb_7_512 );

                secondValue.setText( jsonasynctask.allLux.get( i ) );
                secondValue2.setText( "Lux" );
                secondDevice.setText( jsonasynctask.allDevice.get( i ) );

                secondValue.setVisibility( View.VISIBLE );
                secondValue2.setVisibility( View.VISIBLE );

                secondImage.setColorFilter( Color.parseColor( "#FFFFFF" ) );

            } else if (jsonasynctask.allMode.get( i ).equals( "3" )) {

                linear2.setBackgroundColor( Color.parseColor( "#4FC1E9" ) );
                secondValue.setBackgroundColor( Color.parseColor( "#3BAFDA" ) );
                secondValue2.setBackgroundColor( Color.parseColor( "#3BAFDA" ) );
                secondDevice.setBackgroundColor( Color.parseColor( "#3BAFDA" ) );
                secondView.setBackgroundColor( Color.parseColor( "#4FC1E9" ) );

                secondImage.setImageResource( R.drawable.ic_baseline_meeting_room_24px );

                secondValue.setText( jsonasynctask.allDoor.get( i ) );
                secondValue2.setText( "Door" );
                secondDevice.setText( jsonasynctask.allDevice.get( i ) );

                secondValue.setVisibility( View.VISIBLE );
                secondValue2.setVisibility( View.VISIBLE );

                secondImage.setColorFilter( Color.parseColor( "#FFFFFF" ) );

            } else if (jsonasynctask.allMode.get( i ).equals( "4" )) {

                linear2.setBackgroundColor( Color.parseColor( "#509CEC" ) );
                secondValue.setBackgroundColor( Color.parseColor( "#4A89DC" ) );
                secondValue2.setBackgroundColor( Color.parseColor( "#4A89DC" ) );
                secondDevice.setBackgroundColor( Color.parseColor( "#4A89DC" ) );
                secondView.setBackgroundColor( Color.parseColor( "#509CEC" ) );

                secondImage.setImageResource( R.drawable.vibration );

                secondValue.setText( jsonasynctask.allVib.get( i ) );
                secondValue2.setText( "Vib" );
                secondDevice.setText( jsonasynctask.allDevice.get( i ) );

                secondValue.setVisibility( View.VISIBLE );
                secondValue2.setVisibility( View.VISIBLE );

                secondImage.setColorFilter( Color.parseColor( "#FFFFFF" ) );

            } else if (jsonasynctask.allMode.get( i ).equals( "5" )) {

                linear2.setBackgroundColor( Color.parseColor( "#EC87C0" ) );
                secondValue.setBackgroundColor( Color.parseColor( "#D770AD" ) );
                secondValue2.setBackgroundColor( Color.parseColor( "#D770AD" ) );
                secondDevice.setBackgroundColor( Color.parseColor( "#D770AD" ) );
                secondView.setBackgroundColor( Color.parseColor( "#EC87C0" ) );

                secondImage.setImageResource( R.drawable.ic_magnet );

                secondValue.setText( jsonasynctask.allMag.get( i ) );
                secondValue2.setText( "Mag" );
                secondDevice.setText( jsonasynctask.allDevice.get( i ) );

                secondValue.setVisibility( View.VISIBLE );
                secondValue2.setVisibility( View.VISIBLE );

                secondImage.setColorFilter( Color.parseColor( "#FFFFFF" ) );

            }

        }




     }

    }

}

Solution

  • For the AsyncTask Crash is because maybe onCreateView is called multiple times on ur fragment instance, try this to ensure the for every execute() call u have a new instance

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
    
        View view = inflater.inflate( R.layout.fragment_data_tabel, container, false );
    
        sensor1 = (TextView) view.findViewById( R.id.sensorAlleBox );
    
        new jsonAsynctask( this ).execute();
    
        return view;
    }
    

    On The Other Hand, if u don't like to create new instances all the time make sure to wrap the Call in Async Status Call Check to ensure u don't start the same async while is running:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
    
            View view = inflater.inflate( R.layout.fragment_data_tabel, container, false );
    
            sensor1 = (TextView) view.findViewById( R.id.sensorAlleBox );
    
    if(jsonasynctask.getState() != AsyncTask.Status.RUNNING){
    
            jsonasynctask.execute();
    }
    
            return view;
    
        }
    

    And Finally if your using a Context in the AsyncTask Class make sure to user WeekReference to ensure u don't hold a strong reference in the AsynTask