我搜索了這個問題了一天多時間,但沒有運氣。 我執行完全相同的代碼張貼在這裏:
我對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);
}
的代碼是工作的罰款,直到最後一行,在那裏我需要設置適配器公衆靜態列表初始化在MyCustomFragment
在onCreateView
,這裏我的代碼片段:
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
}
在這裏:
問候,
「Dears」? +1,以防萬一你是編程祖母 – CodyBugstein
看看這個[Tablayout.OnTabSelectedListener](http://stackoverflow.com/questions/17210674/how-to-get-which-fragment-has-been-selected/35127695#35127695)以獲取最新更新。 –