I have tried all the methods mentioned in the following thread How to access Fragment's child views inside fragment's parent Activity? but none of them seem to work for me. Only after trying hard for an hour and a half, I am asking this question.
My CategoriesFragment
public class CategoriesFragment extends Fragment {
private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_categories,container,false);
textView = view.findViewById(R.id.cat_text);
Log.i("Tag",textView.getText().toString());
return view;
}
public TextView getTextView(){
return textView;
}
.
.
.
}
I am opening the categories fragment when the user clicks on a bottom navigation view consisting of a categories tab.
My HomeScreen Activity
public class HomeScreen extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener, CategoriesFragment.OnFragmentInteractionListener{
private BottomNavigationView bottomNavigationView;
private CategoriesFragment categoriesFragment;
private FrameLayout bottomFrameLayout;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
bottomNavigationView = findViewById(R.id.bottom_nav_view);
bottomFrameLayout = findViewById(R.id.bottom_nav_frame);
categoriesFragment = new CategoriesFragment();
bottomNavigationView.setOnNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.bottom_nav_categories :
getSupportFragmentManager().beginTransaction().replace(R.id.bottom_nav_frame,categoriesFragment).addToBackStack(null).commit();
textView = categoriesFragment.getTextView();
textView.setText("DONE");
}
return true;
}
This throws a null pointer exception, as it is not being able to get the Text view. Any help/guidance would be much appreciated :)