2014-01-20 18 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