Android: Fragment cannot get activity

前端 未结 4 1496
情话喂你
情话喂你 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:54

    So i am also using the same fragment transaction manager and the only problem i can see with your code is that you define the TextView tv inside your onCreateView method of your newFragment class and then instantiate it as like this :

    public class AboutFragment extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.newFragment, container, false);
            TextView tv = (TextView) v.findViewById(R.id.textview);
            return v;
        }
    
        public void setMyString(String s) {
            tv.setText(s);
        }
    }
    

    I know it does'nt make really much sense but that is how i am running my code and it works fine :)

提交回复
热议问题