Amira Elsayed Ismail
Amira Elsayed Ismail

Reputation: 9404

null pointer when try to add fragment inside another fragment

I have the following structure

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

when I change the option in spinner i should add (Fragment3 OR Fragment4 OR Fragment5) inside LinearLayout

the code that I am using in the spinner

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

                    }
                });

The code of adding the fragment

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();       
}

I am now getting null pointer exception, and getActivity = null

Upvotes: 0

Views: 73

Answers (1)

Rajesh Ghosh
Rajesh Ghosh

Reputation: 11

Though you haven't share the entire class, seems updateCurrentFragment() and replaceFragment() functions has been written within the Interface BaseFragment or within the individual Fragment1, Fragment2 etc clases, please correct me if I am wrong. On that case getActivity() will always return null as the new Fragment is yet to be added within the current activity. So may be you need to pass though the instance of the current activity (parent activity) to the BaseFragment or Individual claees extends BaseFragment and then use that instance for getActivity like

(instance of BaseFragment).getActivity() rather than only getActivity().

Here is your modified version of code

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();       

}

Let me know if this solves your problem.

Upvotes: 1

Related Questions