[英]Android problems with fragment and fragmentActivity
我创建了带有可滑动查看的选项卡布局,每个选项卡都是一个片段。 因此,当我放置取景器时,我将选项卡更改为片段活动,因此适配器类中的错误表明无法将FollowerFragments()转换为片段。 有什么帮助吗?
适配器类:
public Fragment getItem(int index) {
switch (index) {
case 0:
// Followers fragment activity
return new FollowersFragment();
case 1:
// Best fragment activity
return new BestSellingFragment();
case 2:
// Hot fragment activity
return new HotDealsFragment();
}
return null;
}
跟随片段类:
public class FollowersFragment extends FragmentActivity {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_followers, container, false);
return rootView;
}
ViewFlipper flipper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting View Flipper from main.xml and assigning to flipper reference variable
flipper=(ViewFlipper)findViewById(R.id.viewFlipper1);
flipper.setAutoStart(true);
flipper.setFlipInterval(500);
}
}
public class FollowersFragment extends FragmentActivity {
它应该扩展Fragment
,而不是FragmentActivity
FragmentActivity
是一种支持Loaders
和Fragment
的Activity
。 例如,通过FragmentActivity
您可以访问FramgentManager
或LoaderManager
。 Fragment代表UI
,由FragmentActivity
托管
FollowersFragment
应该扩展Fragment
而不是FragmentActivity
。
为什么? FragmentActivity
是一个包含片段的Activity
,但它本身不是片段,因此无法进行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.