获取新实例片段中的视图返回null

时间:2015-02-26 08:39:31

标签: java android android-fragments

当项目点击它调用函数ListView.OnItemClickListener时,我有selectItem()我在其中创建了一个新的片段实例

private void selectItem(int position, String groupId) {

     some code
     .......

    // Create a new fragment
    Fragment _fragment = MainGroupFragment.newInstance(mActivity, MainGroupId, groupId);

    View _view = _fragment.getView();//this return null

    ........
    some code
}

代码MainGroupFragment

public class MainGroupFragment extends Fragment {

public static final String TAG = "MainGroupFragment";
public static final String ARG_STRING_MAIN_GROUP = "main_group_id";
public static final String ARG_STRING_ITEM_GROUP = "item_group";
public static final String ARG_STRING_ARR_IMAGES = "images";
private Random mRandom;
private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>();
private DisplayImageOptions options;
StaggeredGridView lStaggeredView;
ArrayList<String> lImageUrls = null;
private View lView;

public static final MainGroupFragment newInstance(Activity mActivity, String pMainGroup, String pGroupId){
    MainGroupFragment _fragment = new MainGroupFragment();

    Bundle _bundle = new Bundle();
    _bundle.putString(MainGroupFragment.ARG_STRING_MAIN_GROUP, pMainGroup);
    ArrayList<String> _images = null;
    _images = ItemORM.selectImagesByGroup(mActivity, pGroupId);
    _bundle.putStringArrayList(MainGroupFragment.ARG_STRING_ARR_IMAGES, _images);
    _fragment.setArguments(_bundle);


    return _fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mRandom = new Random();
    options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.ic_img_placeholder)
            .showImageForEmptyUri(R.drawable.ic_img_not_available)
            .showImageOnFail(R.drawable.ic_img_broken)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}

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

    //@TODO create gridview here
    View rootView = inflater.inflate(R.layout.fr_main_group_grid, container, false);
    lStaggeredView = (StaggeredGridView) rootView.findViewById(R.id.grid_view);
    Bundle args = getArguments();
    lImageUrls = args.getStringArrayList(ARG_STRING_ARR_IMAGES);
    String _groupName = args.getString(ARG_STRING_ITEM_GROUP);

    View _rooHeader = inflater.inflate(R.layout.fr_main_group_grid_item_header_footer, container, false);
    TextView _header = (TextView) _rooHeader.findViewById(R.id.txt_title);
    _header.setText(_groupName);

    lStaggeredView.addHeaderView(_header);
    lStaggeredView.setAdapter(new ImageAdapter());

    setFragmentView(rootView);

    return rootView;

}

..... 
some code
}

当我从新创建的片段中获取视图时,它返回null。我做错了吗?我是新的,仍然在探索。任何帮助将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:0)

片段在附加到片段活动之前不会创建视图。那就是调用onCreateView时。

熟悉片段生命周期http://developer.android.com/reference/android/app/Fragment.html