当我向上选择选项卡时,它可以工作,但是当我返回时,内容变为白色。LogCat 显示:
致命异常:main java.lang.IllegalArgumentException:找不到 id 0x7f040070 的视图(com ..:id/list_news_frame_*) 为片段 FeedPlayerNews*Fragment{4055c188 #4 id=0x7f040070} 在 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) 在 android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.setCurrentItemInternal(ViewPager.java:523)java:495) 在 android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:476) 在 com。..viewpager.TabsAdapter.onTabSelected(TabsAdapter.java:76) 在 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) 在 android.view.View$PerformClick.run( View.java:9112) 在 android.os.Handler.handleCallback(Handler.java:587) 在 android.os.Handler.dispatchMessage(Handler.java:92) 在 android.os.Looper.loop(Looper.java:130) ) 在 android.app.ActivityThread.main(ActivityThread.java:3835) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:507) 在 com。 android.internal.os。ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 在 dalvik.system.NativeStart.main(Native Method)
我的课:
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);
}