如何更改片段文本?

时间:2015-07-10 21:12:01

标签: java android

这是我的Fragment类,我在xmlfile中有一个textView

如何通过setText更改TextView

public class Frist extends Fragment   {

    TextView text;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_frist, container, false);
        TextView t = (TextView) this.getView().findViewById(R.id.textView1);
        t.setText("SOME TEXT");

        return rootView;

    }
}

1 个答案:

答案 0 :(得分:2)

而不是

TextView t = (TextView) this.getView().findViewById(R.id.textView1);

你应该在你膨胀的视图上调用findViewById

TextView t = (TextView) rootView.findViewById(R.id.textView1);