1

I have a list in a table fragment with delete button in each row.Whenever I tap to delete I am getting

java.lang.NullPointerException: Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim' on a null object reference

I have used this code example to reload the page. And I found that my fragment is null. I don't know how to resolve this issue. Also how to change it to support.v4.app or simply FragmentManager.

This is my code

   Fragment frg = null;           
    frg = getFragmentManager().findFragmentById(R.id.table_fragment_container);
    Log.v("frag",""+frg);  //this is null 
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg); 
ft.commit();  
Community
  • 1
  • 1
Elizabeth
  • 1,399
  • 1
  • 13
  • 25

3 Answers3

0

I think you did not set "Your_Fragment_TAG" tag in onCreateView() of your fragment, please check. Or you can use below code for fragment reloading.

Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(your_fragment_container);

if (currentFragment instanceof "fragment_layout_instance") {

FragmentTransaction fragTransaction =   (getActivity()).getFragmentManager().beginTransaction();
     fragTransaction.detach(currentFragment);
     fragTransaction.attach(currentFragment);
     fragTransaction.commit();

}

}
musica
  • 1,373
  • 3
  • 15
  • 34
EKN
  • 1,886
  • 1
  • 16
  • 29
0

Create an object of your fragment and replace "fragment_layout_instance" with you fragment object.

EKN
  • 1,886
  • 1
  • 16
  • 29
  • Like this ri8 TableViewFragment mTableViewFragment = new TableViewFragment(); and replaced if (currentFragment instanceof mTableViewFragment ) {} but still error – Elizabeth Oct 21 '16 at 07:47
0

I solved this by adding TAG

Fragment frg = null;
 frg = getFragmentManager().findFragmentByTag("tabFrag");                                                       
final FragmentTransaction ft = getFragmentManager().beginTransaction();   
ft.detach(frg);
ft.attach(frg);
ft.commit();

and while calling this fragment i added TAG

fragmentTransaction.replace(R.id.RLfragment_container, mTableViewFragment, "tabFrag");
Elizabeth
  • 1,399
  • 1
  • 13
  • 25