今天遇到的问题。Activity向Fragment传递数据时,要注意的问题:在Fragment中用于显示Activity传递过来的数据的组件在findViewById时要写 在onCreateView方法中,不然会有错。如写成
<span style="font-size:18px;"> public class Fragment01 extends Fragment{
TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = View.inflate(getActivity(), R.layout.fragment01,null);
tv = (TextView) v.findViewById(R.id.tv);
return v;
}
public void setText(String text){
tv.setText(text);
}
}
</span>
Fragment数据更新技巧
本文介绍了一个常见的Android开发问题——如何正确地在Fragment中更新由Activity传递过来的数据。关键在于确保在onCreateView方法中使用findViewById来获取视图元素,从而避免潜在的空指针异常等问题。
2177

被折叠的 条评论
为什么被折叠?



