简体   繁体   English

我无法使用片段从我的活动中访问片段布局内的文本视图

[英]I am not being able to access the text view inside my fragment layout from my Activity using the fragment

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. 抛出空指针异常,因为它无法获取Text视图。 Any help/guidance would be much appreciated :) 任何帮助/指导将不胜感激:)

The commit() function does not work synchronously, so your fragment will not be ready directly after the call to it. commit()函数无法同步工作,因此您的片段在调用后将无法立即准备就绪。

Taken from the documentation. 取自文档。

Schedules a commit of this transaction. 安排此事务的提交。 The commit does not happen immediately; 提交不会立即发生; it will be scheduled as work on the main thread to be done the next time that thread is ready. 它将在下次线程准备好时安排为主线程上的工作进行安排。


Use commitNow() as this works synchronously and the fragment is fully brought to the lifycycle state of their host. 使用commitNow()因为它可以同步工作,并且该片段已完全进入其主机的lifycycle状态。

See https://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#commitnow for further information 有关更多信息,请参见https://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#commitnow

当您可以将字符串作为参数传递给fragment的公共方法以更改文本时,为什么要从活动中获取textview和设置文本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM