Search code examples
androidandroid-fragmentstextviewsettext

Using setText() from a fragment to modify text in the same fragment


I have one activity that inflate 3 different fragments on it UI. One of this fragment (myTable) have 3 TextViews that I need to modify.

The fact is that I use this same fragment from different activities, so I need to perform this text change directly from the fragment.

I'm doing this:

int d_ex=145909;

@Override   
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
barEx = (TextView) getActivity().findViewById(R.id.tvExQty);
barEx.setText(String.valueOf(d_ex));  // app crash here!!!!!!!
}

I changed the same instructions and paste them on another methods (like onCreateView()) but I get the same mistake.

What I'm doing wrong?

Thanks in advance.


Solution

  • as far as I understood.

    myTable have the TextView you want to change and you want to change it inside myTable itself.

    So you should do it in the onCreateView like this:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View v = inflater.inflate(R.layout.frag_my_table, null);
                TextView txt = (TextView) v.findViewById(R.id.txExQty);
                txt.setText(Integer.toString(d_ex));