我在(ViewPager)findViewById上得到null

时间:2015-10-23 20:27:39

标签: android android-layout android-intent android-activity android-viewpager

我不知道为什么,但我在这行代码上得到null:

mViewPager = (ViewPager) findViewById(R.id.pager_wonder);

我试过的一切都让我无效,我不知道为什么......

这是我的代码:

public class WonderActivity extends FragmentActivity implements ActionBar.TabListener, Serializable {
    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;
    Intent intent;
    Wonder wonder;

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

        intent = getIntent();
        setContentView(R.layout.wonder_info_fragment);

        wonder = intent.getParcelableExtra("wonder");

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("");


        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager_wonder);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
        }


    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                InfoFragmentsWonder infoFragments = new InfoFragmentsWonder();
                infoFragments.setWonder(wonder);
                Fragment fragment = infoFragments;
                return fragment;
            } else {
                return new MapFragmentsWonder();
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
                case 2:
                    return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}

这里是我的xml,其中有一个id为R.id.pager_wonder:

<!-- BEGIN_INCLUDE (view_pager_layout) -->
<android.support.v4.view.ViewPager
    android:id="@+id/pager_wonder"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"/>

所以,如果有人知道我该如何解决这个问题......请告诉我。

0 个答案:

没有答案