SherlockFragment上的RuntimeException

时间:2013-03-12 07:51:29

标签: android actionbarsherlock android-viewpager

我正在关注此示例.. https://bitbucket.org/owentech/abstabsviewpager/src   我尝试在setOnClickListener方法中为Button实施OnCreate并将其强制关闭。

注意:没有实施Button工作正常..

代码:

public class ABSTabsViewPagerActivity extends SherlockFragmentActivity {

ViewPager mViewPager;
TabsAdapter mTabsAdapter;

Button b;

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

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);

    setContentView(mViewPager);
    b = (Button) findViewById(R.id.buttonA);
    b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Button Clicked",
                    Toast.LENGTH_SHORT).show();
        }
    });

    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mTabsAdapter = new TabsAdapter(this, mViewPager);

    mTabsAdapter.addTab(bar.newTab().setText("Fragment 1"),
            FragmentOne.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Fragment 2"),
            FragmentTwo.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Fragment 3"),
            FragmentThree.class, null);

}

public static class TabsAdapter extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);

    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
    }

    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
    }

    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }

    public void onPageScrollStateChanged(int state) {
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}

}

2 个答案:

答案 0 :(得分:1)

您的setContentView(mViewPager)只有viewpager。所以目前的布局中没有按钮。你不能得到这样的按钮b = (Button) findViewById(R.id.buttonA);

设置完整的布局,其中也包含xml中的按钮。

答案 1 :(得分:1)

很明显,您尝试通过调用Button引用的findViewById()不在根布局中,因为根ViewViewPager 。您应该创建一个ViewPagerButton都存在的布局。希望这会有所帮助。