I getFragmentManager().findFragmentById trying to change the fragment fields dynamically, but constantly getFragmentManager().findFragmentById returns NULL . If you use a static fragment, it works without problems. I can not understand what the problem is, I tried a lot of options.

 public class DetailActivity extends Activity{ FilmList mFilmList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); mFilmList = (FilmList)getIntent().getParcelableExtra("FilmList"); int position = getIntent().getIntExtra("position", 0); Fragment fragment; FragmentTransaction fragmentTransaction; fragment = new ViewFragment(); fragmentTransaction = this.getFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.frame, fragment); fragmentTransaction.commit(); HashMap<String, Object> map = mFilmList.getList().get(position); fragment = getFragmentManager().findFragmentById(R.id.frame); ((TextView) fragment.getView().findViewById(R.id.ftext1)) .setText((String) map.get(FilmModel.sNAME)); /* ((TextView) fragment1.getView().findViewById(R.id.ftext2)) .setText((String)map.get(FilmModel.sTIME)); ((TextView) fragment1.getView().findViewById(R.id.ftext3)) .setText((String)map.get(FilmModel.sDESC));*/ } } public class ViewFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.view_fragment, null); setRetainInstance(true); return view; } } 

XML: Activity_detail

 <?xml version="1.0" encoding="utf-8"?> 

 <fragment android:id="@+id/fragment1" android:name="ua.test.wantedy.test.ViewFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" tools:layout="@layout/view_fragment"> </fragment> <FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="wrap_content" tools:layout="@layout/view_fragment" android:layout_weight="1"> </FrameLayout> 

XML: view_fragment

 <?xml version="1.0" encoding="utf-8"?> 

 <TextView android:id="@+id/ftext1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is Title" android:textSize="35sp" /> <TextView android:id="@+id/ftext2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is Time" /> <TextView android:id="@+id/ftext3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is Description" android:textSize="25sp" /> <ImageView android:id="@+id/fimage" android:layout_width="300dp" android:layout_height="200dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:contentDescription="Img" /> 

The only thought is that at the moment of accessing the fragment it is probably not created yet? Although it seems like I should have ..

  • one
    Adding a fragment to the markup is a bad idea. Try to remove it from there and add it only in the code. - Yuriy SPb
  • if you dynamically add a fragment and immediately call it View - NullPointerExeption takes off, as it does not have time to process yet. You need to call it later (on a click, for example), or add a bunch try{}catch{} , which truth also does not always work correctly. - Jarvis_J
  • @AbrogPetrovich Please post your comment as a response. - Nicolas Chabanovsky

1 answer 1

If you dynamically add a fragment and immediately call it View - NullPointerExeption takes off, as it does not have time to process yet. You need to call it later (on a click, for example), or add a bunch try{}catch{} , which truth also does not always work correctly.
In your case, you can transfer the text to the fragment at the time of its creation, through

 public static MyFragment newInstance(String txt) { Bundle args = new Bundle(); args.putInt("text", txt); MyFragment fragment = new MyFragment(); fragment.setArguments(args); return fragment; } 

and in onCreateView() call the text via getArguments().getString("text")

In Activiti, a new fragment is added as follows: MyFragment.newInstance(myText); instead of new MyFragment

  • Thanks for the answer. I thought so. Fragment in the markup, because I tried both options and static in this case works correctly, but the dynamics crashes. Now figured it out. - Serg