Android 如何将操作栏添加到滑动视图?
我一直在跟踪创建一个包含片段的滑动视图 特别是,我使用了代码块Android 如何将操作栏添加到滑动视图?,android,android-actionbar,swipe,Android,Android Actionbar,Swipe,我一直在跟踪创建一个包含片段的滑动视图 特别是,我使用了代码块 public class CollectionDemoActivity extends FragmentActivity { // When requested, this adapter returns a DemoObjectFragment, // representing an object in the collection. DemoCollectionPagerAdapter mDemoColl
public class CollectionDemoActivity extends FragmentActivity {
// When requested, this adapter returns a DemoObjectFragment,
// representing an object in the collection.
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection_demo);
// ViewPager and its adapters use support library
// fragments, so use getSupportFragmentManager.
mDemoCollectionPagerAdapter =
new DemoCollectionPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
}
}
// Since this is an object collection, use a FragmentStatePagerAdapter,
// and NOT a FragmentPagerAdapter.
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
// Our object is just an integer :-P
args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return 100;
}
@Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
// Instances of this class are fragments representing a single
// object in our collection.
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// The last two arguments ensure LayoutParams are inflated
// properly.
View rootView = inflater.inflate(
R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
Integer.toString(args.getInt(ARG_OBJECT)));
return rootView;
}
}
并扩展它以满足我的需要
如何修改此选项,以便在屏幕顶部有一个操作栏,其中仅包含活动名称(在中间)、应用程序图标和返回父活动的箭头(在左侧)
我试着加上
final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
在上面的代码中创建CollectionDemoActivity onCreate,但这会导致我的应用程序崩溃
编辑:
My styles.xml包括
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<style name="AppTheme" parent="AppBaseTheme">
我的舱单包括
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
…在应用程序内部具有活动标记。所有活动都不会覆盖应用程序级别的安卓主题集。父活动有一个操作栏,但子活动(我在本文中询问的滑动活动)没有。您的问题本质上非常简单,因为它与滑动视图几乎没有关系:您只需要在您的FragmentActivity中有一个操作栏 : 注意:如果要实现包含操作栏的活动,则应改用ActionBarActivity类,它是该类的子类,因此允许在API级别7及更高级别上使用片段API
让您的活动扩展ActionBarActivity,并使用
getSupportActionBar()
获取对ActionBar的引用您的主题需要支持使用ActionBar。如果您只是尝试在没有支持的主题的情况下访问它,那么您将得到NullPointerException并崩溃。我认为它已经崩溃了:例如,父活动(启动滑动活动的活动)有一个操作栏。将编辑帖子并添加更多信息。主题可以逐个活动,但如果您确定此特定活动具有主题,那么请确保我们可以查看您拥有的其他信息。如果您使用的是AppCompat,则应改为使用getSupportActionBar()
。您还应使用ActionBarActivity。