Cannot cast from fragment

Kristopher picture Kristopher · Mar 8, 2013 · Viewed 7.4k times · Source

I've a two fragments defined in the layout of my MainActivity and set by setContentView. I'd like to call some methods from these fragments, but I don't know how to acces them. Eclipse says: "Cannot cast form Fragment to ListFileFrgament".

public class MainActivity extends FragmentActivity {

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

    setContentView(R.layout.activity_main);

    FileListFragment listFragment = (FileListFragment) getFragmentManager().findFragmentById(R.id.list_fragment);  
     // cannot casr here
    PlayerFragment playerFragment = (PlayerFragment) getFragmentManager().findFragmentById(R.id.player_fragment); 
     // cannot cast here
    }


}

Answer

JafarKhQ picture JafarKhQ · Mar 8, 2013

use getSupportFragmentManager() instead of getFragmentManager()