Could not launch GridView in ViewPager. Pictures for GridView are taken from the folder on the SD. The application starts, but there is nothing on the page where the GridView should be. The logs show that even the adapter for the GridView does not start. The sample code for the GridView (below) in a separate application works. Where am I stupid?

gridview fragment code:

public class FirstFragment extends Fragment { private Context context; GridView mGrid; private static final String TAG = "myLogs"; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG,"onCreat first"); File dir = new File(Environment.getExternalStorageDirectory(), "ICOIN/"); File[] filesArray = dir.listFiles(); if (filesArray != null) { Adapter adapter = new AdapterGridView(getActivity(),filesArray); Log.i(TAG,"adapter"); mGrid.setAdapter((ListAdapter) adapter); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //задаем разметку фрагменту final View view = inflater.inflate(R.layout.activity_first_fragment, container, false); Log.i(TAG,"viewFirst"); //ну и контекст, так как фрагменты не содержат собственного context = view.getContext(); mGrid = (GridView)view.findViewById(R.id.gridView1); return view; } 

gridview adapter code:

 public class AdapterGridView extends ArrayAdapter<File> { LayoutInflater mInflater; Picasso mPicasso; private static final String TAG = "myLogs"; public AdapterGridView(Context context, File[] objects) { super(context, R.layout.list_item, objects); mInflater = LayoutInflater.from(context); mPicasso = Picasso.with(context); } @Override public View getView ( final int position, View convertView, ViewGroup parent){ View view = convertView; if (view == null) { view = mInflater.inflate(R.layout.list_item, parent, false); Log.i(TAG,"adapter grid"); } ImageView imageView = (ImageView) view.findViewById(R.id.imageView); mPicasso.load(getItem(position)).fit().centerCrop().into(imageView); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/*"); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(getItem(position))); getContext().startActivity(Intent.createChooser(intent, "")); } }); return view; } 
  • one
    onCreate after all is caused earlier, than onCreateView? - Vladyslav Matviienko 6:09 pm

1 answer 1

First, I forgot to give the application rights to read the sd card, it happens) Secondly, as metalurgus said, "is onCreate called before, than onCreateView?" you need to transfer all the code from onCreat to OnCreatView