Android: Fragment cannot get activity

前端 未结 4 1491
情话喂你
情话喂你 2020-12-10 02:31

I have an activity which does a fragment transaction

DetailFragment newFragment = new DetailFragment();
transaction.replace(R.id.mylist, newFragment);
transa         


        
4条回答
  •  孤城傲影
    2020-12-10 02:39

    Your Fragment hasn't attached to the Activity yet, which also means your layout hasn't been inflated yet (See the Fragment lifecycle). The best solution here would be to add your String value as an argument to the Fragment via the Fragment.setArguments(Bundle) method. This can be retrieved in the receiving Fragment via the Fragment.getArguments() method.

提交回复
热议问题