我在我的MainActivity
的布局中定义了两个片段,并且由setContentView
进行了设置。我想从这些片段中调用一些方法,但我不知道如何访问它们。 Eclipse说:“无法将表单片段转换为ListFileFrgament”。的getFragmentManager()
无法从片段进行投射
8
A
回答
21
使用getSupportFragmentManager()
,而不是确保您的进口匹配 - 这可能是因为FileListFragment/PlayerListFragment延长android.support.v4.app.Fragment而getFragmentManager()返回findFragmentById android.app。分段。
使用getSupportFragmentManager()
返回android.support.v4.app.Fragment实例。
+1
你能解释一下为什么吗? – TrungDQ 2015-07-18 03:53:36
5
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
}
}
相关问题
- 1. 无法投射C#
- 2. java.lang.ClassCastException:android.app.Application无法投射
- 3. 无法投射MS.Internal.NamedObject
- 4. Android:无法从视图投射到MapView
- 5. 无法从Throwable投射到MyException
- 6. 无效投射例外 - 投射无效
- 7. Foo无法投射到Foo
- 8. .HashMap无法投射到android.database.Cursor
- 9. 无法投射COM对象
- 10. 无法投射清单
- 11. 无法投射左连接
- 12. 无法投射字符串
- 13. 无法投射通用集?
- 14. MockView无法投射到android.view.ViewGroup
- 15. 使用Java进行反射投射和重载方法调度
- 16. 在LINQ查询中进行投射
- 17. 使用TValue进行Delphi接口投射
- 18. 如何在排序时进行投射?
- 19. 无法投射异常类型对象
- 20. Xcode Segue“无法投射类型的值”
- 21. 无法投射EntityEntry当前值
- 22. 无法投射“System.Collections.Generic.HashSet”类型的对象
- 23. 无法在silverlight中投射类型isse
- 24. 异常android.support.multidex.MultiDexApplication无法投射类
- 25. Mongo无法将HashMap投射到BasicDBObject
- 26. 无法投射WhereSelectListIterator类型的对象
- 27. 对象无法投射到ArrayList
- 28. Scala/Play ClassCastException:无法投射java.lang.Integer到java.lang.Long]
- 29. 无法投射任何东西?到NSSize?
- 30. android.app.Application无法投射到活动
检查您的布局文件。检查你的ID。 – hakiko 2013-03-08 19:47:55