1

在我的日曆應用程序中,我使用ActionBarSherlock顯示兩個選項卡 - 1)日曆2)轉換器。在ActionBar菜單項的單擊事件中,我使用SlidingMenu抽屜來顯示該月的事件摘要。SlidingMenu with ViewPager和ActionBarSherlock標籤

我想專門使用viewpager日曆選項卡,我已經實現了這種方式:

Main.java:

public class BaseActivity extends SlidingSherlockFragmentActivity 
{ 

    private ViewPager pager; 
    CalendarFragmentPagerAdapter mPagerAdapter; 
    private static final int MONTHS_LIMIT = 5; 

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

     setTitle(R.string.app_name); 

     setContentView(R.layout.main); 
     getSupportFragmentManager() 
     .beginTransaction() 
     .replace(R.id.main, new CalendarFragment()) 
     .commit(); 

     setBehindContentView(R.layout.content_frame); 
     getSupportFragmentManager() 
     .beginTransaction() 
     .replace(R.id.content_frame, new EventsSummary()) 
     .commit(); 

     setSlidingActionBarEnabled(false); 

     final ActionBar bar = getSupportActionBar(); 
     bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     bar.setHomeButtonEnabled(true); 

     ActionBar.Tab calendar = bar.newTab(); 
     ActionBar.Tab converter = bar.newTab(); 

     calendar.setText(this.getResources().getString(R.string.calendar)); 
     converter.setText(this.getResources().getString(R.string.converter)); 

     calendar.setTabListener(new MyTabListener()); 
     converter.setTabListener(new MyTabListener()); 

     bar.addTab(calendar); 
     bar.addTab(converter); 

     // customize the SlidingMenu 
     SlidingMenu sm = getSlidingMenu(); 
     sm.setShadowWidthRes(R.dimen.shadow_width); 
     sm.setShadowDrawable(R.drawable.shadow); 
     sm.setBehindOffsetRes(R.dimen.actionbar_home_width); 
     sm.setSlidingEnabled(false); 

     mPagerAdapter = new CalendarFragmentPagerAdapter(getSupportFragmentManager()); 
     pager = (ViewPager) this.findViewById(R.id.viewpager); 
     pager.setAdapter(mPagerAdapter); 
     pager.setCurrentItem(MONTHS_LIMIT/2); 


    } 


    private class MyTabListener implements ActionBar.TabListener 
    { 

     @Override 
     public void onTabSelected(Tab tab, FragmentTransaction ft) { 
      if(tab.getPosition()==0) 
      { 
       CalendarFragment frag = new CalendarFragment(); 
       ft.replace(R.id.main, frag); 
      } 
      else 
      { 
       ConverterFragment frag = new ConverterFragment(); 
       ft.replace(R.id.main, frag); 
      } 
     } 
     @Override 
     public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
      // TODO Auto-generated method stub 
     } 
     @Override 
     public void onTabReselected(Tab tab, FragmentTransaction ft) { 
      // TODO Auto-generated method stub 
     } 
    } 

    public boolean onOptionsItemSelected(MenuItem item) { 
     Intent newActivity; 
     // Handle item selection 
     switch (item.getItemId()) { 
      case R.id.menu_slidingmenu: 
       toggle(); 
       return true; 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 



    public class CalendarFragmentPagerAdapter extends FragmentPagerAdapter { 

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

     @Override 
     public Fragment getItem(int position) { 
      CalendarFragment fragment = new CalendarFragment(); 
      Bundle args = new Bundle(); 
      args.putInt("offset", position - MONTHS_LIMIT/2); 
      fragment.setArguments(args); 
      return fragment; 
     } 

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

    } 
} 

CalendarFragment.java:

public class CalendarFragment extends Fragment 
{ 

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

      Bundle args = getArguments(); 
      int offset = args.getInt("offset"); //throws NullPointerException 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

      View v = inflater.inflate(R.layout.simple_calendar_view, container, false); 

      ..... 

      return v; 
     } 

     @Override 
     public void onSaveInstanceState(Bundle outState) { 
      super.onSaveInstanceState(outState); 
      outState.putBoolean("dummy", true); 
     } 


} 

問題是在CalendarFragment的這一行:

int offset = args.getInt("offset"); 

它引發錯誤NullPointerException和調試顯示這是因爲Bundle args = getArguments()爲空。

我試圖把斷點在此行中PagerAdapter:

Bundle args = new Bundle(); 

,但它永遠不會到達這裏和應用程序崩潰扔NullPointerException

我哪裏錯了?

回答

0

這裏

getSupportFragmentManager() 
     .beginTransaction() 
     .replace(R.id.main, new CalendarFragment()) 
     .commit(); 

這裏

@Override 
public void onTabSelected(Tab tab, FragmentTransaction ft) { 
    if (tab.getPosition() == 0) { 
     CalendarFragment frag = new CalendarFragment(); 
     ft.replace(R.id.main, frag); 
    } else { 
     ConverterFragment frag = new ConverterFragment(); 
     ft.replace(R.id.main, frag); 
    } 
} 

你只是不帶任何參數創建一個空的CalendarFragment實例。

所以,你的活動推出,不帶參數創建一個空的片段,然後試圖讓那些

+0

感謝您指出崩潰時。我還要如何確保FragmentPagerAdapter將參數傳遞給CalendarFragment而不再次被替換? – input

相關問題