ActionBar Sherlock ViewPager片段

时间:2013-01-21 17:10:27

标签: android actionbarsherlock fragment

当我向上选择标签时,它可以工作,但当我返回时,内容变为白色。 LogCat显示:

  

FATAL EXCEPTION:main java.lang.IllegalArgumentException:无视图   找到ID为0x7f040070(com。:id / list_news_frame_ *)的片段   FeedPlayerNews *片段{4055c188#4 id = 0x7f040070} at   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)   在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)   在   android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)   在   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)   在   android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)   在   android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)在android.support.v4.view.ViewPager.populate(ViewPager.java:1012)at at   android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:523)   在   android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:495)   在   android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:476)   at com。 .viewpager.TabsAdapter.onTabSelected(TabsAdapter.java:76)at at   com.actionbarsherlock.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:526)   在   com.actionbarsherlock.internal.app.ActionBarImpl $ TabImpl.select(ActionBarImpl.java:907)   在   com.actionbarsherlock.internal.widget.ScrollingTabContainerView $ TabClickListener.onClick(ScrollingTabContainerView.java:504)   在android.view.View.performClick(View.java:2506)at   android.view.View $ PerformClick.run(View.java:9112)at   android.os.Handler.handleCallback(Handler.java:587)at   android.os.Handler.dispatchMessage(Handler.java:92)at   android.os.Looper.loop(Looper.java:130)at   android.app.ActivityThread.main(ActivityThread.java:3835)at   java.lang.reflect.Method.invokeNative(Native Method)at   java.lang.reflect.Method.invoke(Method.java:507)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:864)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)at   dalvik.system.NativeStart.main(原生方法)

我的课程:

public 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>();
    ProgressDialog dialog;

    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();
    }

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

    public SherlockFragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return (SherlockFragment) 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) {
        mViewPager.setCurrentItem(tab.getPosition());
        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) {
    }

}

更新:

持有viewpager的方法:

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

        ConnectionChangeReceiver cm = new ConnectionChangeReceiver();
        cm.onReceive(this, getIntent());

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

        RssReaderTask.setRssUrl(this,
                "http://url?type=rss");
        SchedulerManager.getInstance().saveTask(this, "0 9-20 * * 1,2,3,4,5",
                RssReaderTask.class);
        SchedulerManager.getInstance().restart(this, RssReaderTask.class);


        NotifyClass.getInstance().setSlideCreate(true);

        if (!isOnline()) {
            showDialog(DIALOG_ERROR_CONNECTION);
            Log.d("TAG", "TRUE");
        }

        if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE) {
            // do nothing
        } else {
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        globalvar = new GlobalVariable();
        globalvar.set_radio_encours_global(false);
        globalvar.setOnHold_global(false);

        mTabsAdapter = new TabsAdapter(this, mViewPager);

        bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayShowHomeEnabled(false);

        mTabsAdapter.addTab(bar.newTab().setText("Tab1"),
                Tab1.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Tab2"), Tab2.class,
                null);
        mTabsAdapter.addTab(bar.newTab().setText("Tab3 "),
                Tab3.class, null);
        mTabsAdapter.addTab(bar.newTab().setText("Tab4"),
                Tab4.class, null);

    }

1 个答案:

答案 0 :(得分:0)

你的课程没问题,它应该正常工作,我使用同样的工具......我认为你的活动/片段中存在一个问题,它存放了viewpager,所以我建议你去修复你的onCreate / onCreateView方法。
如果你找不到任何东西,如果命名方法不是错误的原因,请发布此方法的代码或类的代码。