尝试在另一个片段内添加片段时为空指针

时间:2014-09-17 10:47:33

标签: android android-fragments android-spinner

我有以下结构

Activity 
   |________ Spinner
   |________ TabHost
                |_______ Fragment1
                             |________ LinearLayout
                |_______ Fragment2

当我在spinner中更改选项时,我应该在LinearLayout中添加(Fragment3 OR Fragment4 OR Fragment5)

我在微调器中使用的代码

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view,
                            int position, long id) {
                        Fragment1 fragment = (BaseFragment) mTabFragments.get(mTabHost.getCurrentTab());
                        fragment.updateCurrentFragment(position);
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }
                });

添加片段的代码

public void updateCurrentFragment (int index) {

    switch (index) {
    case 0:
        replaceFragment(new Fragment3());
        break;
    case 1:
        replaceFragment(new Fragment4());
        break;
    case 2:
        replaceFragment(new Fragment5());
        break;
    }
}

public void replaceFragment (Fragment newFragement) {

    FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
    transaction.add(R.id.fragment_layout, newFragement);
    transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    transaction.commit();       
}

我现在得到空指针异常,并且getActivity = null

1 个答案:

答案 0 :(得分:1)

虽然你还没有共享整个类,但是看来updateCurrentFragment()和replaceFragment()函数已经在Interface BaseFragment中编写,或者在单独的Fragment1,Fragment2等文章中编写,如果我错了,请纠正我。在这种情况下,getActivity()将始终返回null,因为尚未在当前活动中添加新的Fragment。因此,您可能需要将当前活动的实例(父活动)传递给BaseFragment,或者个人claees扩展BaseFragment,然后将该实例用于getActivity,如

(BaseFragment的实例).getActivity()而不仅仅是getActivity()。

以下是您修改后的代码版本

spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                        int position, long id) {
                    Fragment1 fragment = (BaseFragment) mTabFragments.get(mTabHost.getCurrentTab());
                    fragment.updateCurrentFragment(**this**,position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });

public void updateCurrentFragment(ParentActivity pAct ,int index){

switch (index) {
case 0:
    replaceFragment(**pAct**,new Fragment3());
    break;
case 1:
    replaceFragment(**pAct**,new Fragment4());
    break;
case 2:
    replaceFragment(**pAct**,new Fragment5());
    break;
}

}

public void replaceFragment((ParentActivity pAct ,Fragment newFragement){

FragmentTransaction transaction = **pAct**.getActivity().getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_layout, newFragement);
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
transaction.commit();       

}

如果这可以解决您的问题,请告诉我。