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 ..
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 bunchtry{}catch{}
, which truth also does not always work correctly. - Jarvis_J