Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using FragmentManager to access fragment methods from an activity

In the below method I'm trying to call a fragments method from within an activity. The class this method is in extends FragmentActivity. Should all my imports for the fragments be from android.support.v4? The error is on the last line "Cannot cast from Fragment to NasaDailyImage" any ideas?

  public void onRefresh(View view){
       android.app.FragmentManager fragmentManager=getFragmentManager();
       NasaDailyImage NasaDailyFragment;
       NasaDailyFragment=(NasaDailyImage)fragmentManager.findFragmentById(R.id.fragment_iotd);  //cannot cast from Fragment to NasaDailyImage

 //NasaDailyFragment.onRefresh();
   }

As of now all my imports for fragments are from android.support.v4.app except for the FragmentManager, because when I import android.support.v4.app.FragmentManager instead of android.app.FragmentManager then the getFragmentManager() method is no longer available. I tried to get around this by creating a android.support.v4.app.FragmentManager() object but I got an error saying that I cant instantiate it.

like image 992
Tyler Pfaff Avatar asked Jul 18 '12 10:07

Tyler Pfaff


1 Answers

Did you try the public FragmentManager getSupportFragmentManager () method ?

like image 66
AMerle Avatar answered Sep 16 '22 23:09

AMerle