不要缓存您的视图。使用 getView() 方法获取您的根视图。如果还没有,它将被创建。
更改您的线路:
TextView text=(TextView)view.findViewById(R.id.tt);
到:
TextView text=(TextView)getView().findViewById(R.id.tt);
无论如何,将值设置为直接从外部对象查看并不是一个好方法。你可以这样做:
private String mText = null;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// onActivityCreated calls after view creation, and attaching fragment to Activity so it's good place to fill your views with default info
TextView text=(TextView)getView().findViewById(R.id.tt);
text.setText(mText);
}
void setSongList(ArrayList<SongDetails> songinfo) {
View v = getView();
// Cache your text, and set it to TextView only if View already created.
this.mText = "mytext";
if(v != null) {
TextView text=(TextView)getView().findViewById(R.id.tt);
text.setText(mText);
}
}