2014-01-20 16 views
1

逗人,玩片段onTabSelected

我搜索了這個問題了一天多時間,但沒有運氣。 我執行完全相同的代碼張貼在這裏:

Adding Navigation Tabs

我對onTabSelected代碼如下所示:

public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     // Check if the fragment is already initialized 
     if (mFragment == null) { 
      // If not, instantiate and add it to the activity 
      mFragment = Fragment.instantiate(mActivity, mClass.getName()); 
      ft.add(R.id.alert_fragment_container, mFragment, mTag); 
     } else { 
      // If it exists, simply attach it in order to show it 
      ft.attach(mFragment); 
     } 

     // prepare adapter for ExpandableListView 

     Log.i("After Adapter Created", "Passed"); 

     final ExpandableListAdapter expListAdapter = new AlertsAdapter(
       mActivity, myAlerts, violations); 

     Log.i("After Adapter Initialized", "Passed"); 

     ((MyCustomFragment)mFragment).violations.setAdapter(expListAdapter); 
    } 

的代碼是工作的罰款,直到最後一行,在那裏我需要設置適配器公衆靜態列表初始化在MyCustomFragmentonCreateView,這裏我的代碼片段:

public class MyCustomFragment extends Fragment { 

    public MyCustomFragment() { 
    } 

    public static ExpandableListView violations; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_alerts_poi, container, false); 

     violations = (ExpandableListView) rootView.findViewById(R.id.POIAlertList); 

     Log.i("onCreateView POI", "Called"); 

     return rootView; 
    } 
} 

它給Null指針錯誤。用我的調試日誌,我注意到這個日誌Log.i("onCreateView POI", "Called");出現在這個Log.i("After Adapter Initialized", "Passed");之後。這意味着我試圖設置一個片段的適配器尚未初始化。

這是我面對的確切問題,我需要根據onTabSelected中的選項卡選擇數據來提供ExpandableListView

我在做什麼錯了?什麼是最好的解決方案?

public class MainActivity extends FragmentActivity implements ActionBar.TabListener{ 
private ActionBar actionBar; 
private ViewPager mViewPager; 
private AppSectionsPagerAdapter mAppSectionsPagerAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager()); 
    actionBar=getActionBar(); 
    actionBar.setHomeButtonEnabled(false); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mAppSectionsPagerAdapter); 
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
     @Override 
     public void onPageSelected(int position) { 
      actionBar.setSelectedNavigationItem(position); 
     } 
    }); 
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon1).setTabListener(this)); 
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon2).setTabListener(this)); 
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon3).setTabListener(this)); 
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon4).setTabListener(this));  
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // TODO Auto-generated method stub 
    getMenuInflater().inflate(R.menu.action_menu, menu); 
    return true; 
} 

@Override 
public void onTabReselected(Tab tab, FragmentTransaction ft) { 
    // TODO Auto-generated method stub 

} 

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

} 

@Override 
public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
    // TODO Auto-generated method stub 

} 

在這裏:

問候,

+3

「Dears」? +1,以防萬一你是編程祖母 – CodyBugstein

+0

看看這個[Tablayout.OnTabSelectedListener](http://stackoverflow.com/questions/17210674/how-to-get-which-fragment-has-been-selected/35127695#35127695)以獲取最新更新。 –

回答

1

看來你需要一個ViewPager,我只是實現了一個導航選項卡,前幾天,這裏是我的代碼,這4個片段之間的導航是適配器:

public class AppSectionsPagerAdapter extends FragmentPagerAdapter { 
public AppSectionsPagerAdapter(FragmentManager fm) { 
    super(fm); 
} 

@Override 
public Fragment getItem(int i) { 
    switch (i) { 
     case 0: 
      return new Fragment1(); 
     case 1: 
      return new Fragment2(); 
     case 2: 
      return new Fragment3(); 
     case 3: 
      return new Fragment4(); 
    } 
    return null; 
} 

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

} 
+0

我可以在哪裏放置準備和設置適配器的代碼?另外,有沒有解決方案,而不是這個?在這種情況下,我將應用這個解決方案,我需要改變我的結構中的很多東西。爲什麼我的方式錯了? – alawibh

+0

你不需要改變很多東西,適配器在MainActivity中初始化,我不知道你的主要活動是什麼樣子的,所以很難弄清楚你的代碼有什麼問題,在上面的MainActivity中,還有一個onTabSelected()方法,我也跟着你一樣的教程,希望你可以通過比較上面的代碼和你的代碼找到這個bug。 –

+0

我使用'ViewPager'將我的代碼轉換爲新的活動。它適用於第二,第三和第四個選項卡。第一個仍然給我空指針。我將初始化適配器的代碼放入'ViewPager'監聽器的'onPageSelected(int position)'中。我試圖強制偵聽器使用'pageChangeListener.onPageSelected(0);'調用'onPageSelected(int position)'第一個位置= 0,沒有運氣。 – alawibh