5

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.

1
  • 2
    Use getSupportFragmentManager()
    – pawelzieba
    Jul 18, 2012 at 10:39

1 Answer 1

10

Did you try the public FragmentManager getSupportFragmentManager () method ?

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.