Я перепробовал все методы, упомянутые в следующей теме
Как получить доступ к дочерним представлениям фрагмента внутри родительской активности фрагмента? , но ни один из них, похоже, не работает для меня. Только после полутора часов я задаю этот вопрос.
Мой КатегорииФрагмент
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;
}
.
.
.
}
Я открываю фрагмент категории, когда пользователь нажимает на нижнюю панель навигации, состоящую из вкладки категорий.
Мой Домашний экран Активность
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;
}
Это вызывает исключение нулевого указателя, так как он не может получить текстовое представление. Любая помощь / руководство будет высоко ценится:)