从片段内访问变量

时间:2014-04-07 21:05:57

标签: android

我有一个主要活动,包含三个选项卡,每个选项卡都有相应的片段。在每个片段中,都有一个textview。另外,我使用ViewPager来帮助我浏览标签。

当我尝试从片段中访问TextView时,出现NULL POINTER错误。这是我的代码:

片段1(tab1)

public class FragmentA extends Fragment{
TextView tv1;

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

    //******* Error Here **********
    tv1 = (TextView) getView().findViewById(R.id.fragAtv1);


    return parentView;
}

}

主要活动:

public class SwipeTabs extends FragmentActivity implements TabListener{

ViewPager viewPager;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.main_swipe);

    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(new MySwipeAdapter(getSupportFragmentManager()));
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int tab_pos) {
            actionBar.setSelectedNavigationItem(tab_pos);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

    // Initialize action bar with tabs
    actionBar = setupActionBar();
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction arg1) {

}
@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
    viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction arg1) {

}

private ActionBar setupActionBar(){
    ActionBar newAB = getActionBar();
    newAB.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = newAB.newTab();
    tab1.setText("Tab 1");
    tab1.setTabListener(this);
    newAB.addTab(tab1);

    ActionBar.Tab tab2 = newAB.newTab();
    tab2.setText("Tab 2");
    tab2.setTabListener(this);
    newAB.addTab(tab2);

    ActionBar.Tab tab3 = newAB.newTab();
    tab3.setText("Tab 3");
    tab3.setTabListener(this);
    newAB.addTab(tab3);

    return newAB;
}
}

class MySwipeAdapter extends FragmentStatePagerAdapter{
//Construstor: retuns the 
public MySwipeAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    switch(position){
    case 0:
        fragment = new FragmentA();
        break;
    case 1:
        fragment = new FragmentB();
        break;
    case 2:
        fragment = new FragmentC();
        break;
    }
    return fragment;
}
@Override
public int getCount() {
    return 3;
}

}

2 个答案:

答案 0 :(得分:2)

getView()返回之前,您无法访问null并获取非onCreateView()值。这在逻辑上没有意义。相反,这样做:

tv1 = (TextView) parentView.findViewById(R.id.fragAtv1);

你也可以覆盖onViewCreated(),但这更简单。

答案 1 :(得分:1)

而不是使用getView.findviewbyId尝试使用parentView.findViewById。

tv1 = (TextView) parentView.findViewById(R.id.fragAtv1);