简体   繁体   中英

Get view in a new instance fragment returns null

I have ListView.OnItemClickListener when item click it call function selectItem() where i create a new instance of fragment

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
}

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
}

when i get the view from the newly created fragment it return null. am i doing it wrong? im new and still exploring. any help would be appreciated.

thanks.

Fragments do not create their views until they are attached to a Fragment Activity. That is when onCreateView is called.

Familiarise yourself with the fragment life cycle http://developer.android.com/reference/android/app/Fragment.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM