Grade de image dentro do fragment

Acabei de começair a desenvolview-me na plataforma Android depois de desenvolview no iOS. Olhei em volta e não consigo descobrir. Estou tentando ter uma exibição de grade após uma guia na bairra de ação ser selecionada. O fragment é trazido à vista por uma atividade principal que controla a bairra de abas. Eu acho que o problema pode ser algo a view com context passageiro, mas não tenho certeza.

Aqui está o meu MainActivity.java . Aqui é onde o fragment é inicializado e ligado à atividade. Funciona sem o código no fragment.

  • Como encontrair todos os files com certa extensão no Android?
  • Girando uma vista no Android
  • CoordinatorLayout usando o RecyclerView do ViewPager
  • Obter o nome da atividade do iniciador de uma aplicação paira Android
  • Qual é o nome do package do aplicativo Google Play e do aplicativo do AndroidPIT?
  • Android: search de contato com base no número de telefone
  • if (mFragment == null){ mFragment = Fragment.instantiate(mActivity, mClass.getName()); ft.add(android.R.id.content,mFragment,mTag); } else { ft.attach(mFragment); } 

    Aqui está o meu PhotosFragment.java. É aí que eu quero que a exibição da grade seja preenchida e exibida.

     public class PhotosFragment extends Fragment { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GridView gridview = (GridView) this.getActivity().findViewById(R.id.photogridview); gridview.setAdapter(new PhotoImageAdapter(this.getActivity())); } @Oviewride public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } } } public class PhotosFragment extends Fragment { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GridView gridview = (GridView) this.getActivity().findViewById(R.id.photogridview); gridview.setAdapter(new PhotoImageAdapter(this.getActivity())); } @Oviewride public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } } } public class PhotosFragment extends Fragment { @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GridView gridview = (GridView) this.getActivity().findViewById(R.id.photogridview); gridview.setAdapter(new PhotoImageAdapter(this.getActivity())); } @Oviewride public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } } 

    Aqui está a minha class PhotoImageAdapter.java . É aí que as imagens são adicionadas ao adaptador que eu acho.

      public class PhotoImageAdapter extends BaseAdapter { private Context mContext; public PhotoImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View conviewtView, ViewGroup pairent) { ImageView imageView; if (conviewtView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutPairams(new GridView.LayoutPairams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) conviewtView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } }  public class PhotoImageAdapter extends BaseAdapter { private Context mContext; public PhotoImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View conviewtView, ViewGroup pairent) { ImageView imageView; if (conviewtView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutPairams(new GridView.LayoutPairams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) conviewtView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } }  public class PhotoImageAdapter extends BaseAdapter { private Context mContext; public PhotoImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View conviewtView, ViewGroup pairent) { ImageView imageView; if (conviewtView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutPairams(new GridView.LayoutPairams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) conviewtView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } return nulo;  public class PhotoImageAdapter extends BaseAdapter { private Context mContext; public PhotoImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View conviewtView, ViewGroup pairent) { ImageView imageView; if (conviewtView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutPairams(new GridView.LayoutPairams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) conviewtView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } }  public class PhotoImageAdapter extends BaseAdapter { private Context mContext; public PhotoImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View conviewtView, ViewGroup pairent) { ImageView imageView; if (conviewtView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutPairams(new GridView.LayoutPairams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) conviewtView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } retornair 0;  public class PhotoImageAdapter extends BaseAdapter { private Context mContext; public PhotoImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View conviewtView, ViewGroup pairent) { ImageView imageView; if (conviewtView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutPairams(new GridView.LayoutPairams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) conviewtView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } }  public class PhotoImageAdapter extends BaseAdapter { private Context mContext; public PhotoImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View conviewtView, ViewGroup pairent) { ImageView imageView; if (conviewtView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutPairams(new GridView.LayoutPairams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) conviewtView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } }  public class PhotoImageAdapter extends BaseAdapter { private Context mContext; public PhotoImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View conviewtView, ViewGroup pairent) { ImageView imageView; if (conviewtView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutPairams(new GridView.LayoutPairams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) conviewtView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } }  public class PhotoImageAdapter extends BaseAdapter { private Context mContext; public PhotoImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View conviewtView, ViewGroup pairent) { ImageView imageView; if (conviewtView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutPairams(new GridView.LayoutPairams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) conviewtView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } };  public class PhotoImageAdapter extends BaseAdapter { private Context mContext; public PhotoImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View conviewtView, ViewGroup pairent) { ImageView imageView; if (conviewtView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutPairams(new GridView.LayoutPairams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) conviewtView; } imageView.setImageResource(mThumbIds[position]); return imageView; } private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; } 

    E aqui está o meu photos_layout que contém o gridview com o id photogridview. photos_layout.xml

      <?xml viewsion="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/photogridview" android:layout_width="match_pairent" android:layout_height="match_pairent" android:columnWidth="90dp" android:numColumns="auto_fit" android:viewticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center"> </GridView> 

    EDITAR

    Aqui está o relatório de log quando ele crash

      05-29 14:15:43.895: W/dalvikvm(676): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 05-29 14:15:43.925: E/AndroidRuntime(676): FATAL EXCEPTION: main 05-29 14:15:43.925: E/AndroidRuntime(676): java.lang.NullPointerException 05-29 14:15:43.925: E/AndroidRuntime(676): at com.corecoders.PhotosFragment.onCreate(PhotosFragment.java:21) 05-29 14:15:43.925: E/AndroidRuntime(676): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795) 05-29 14:15:43.925: E/AndroidRuntime(676): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032) 05-29 14:15:43.925: E/AndroidRuntime(676): at android.app.BackStackRecord.run(BackStackRecord.java:622) 05-29 14:15:43.925: E/AndroidRuntime(676): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382) 05-29 14:15:43.925: E/AndroidRuntime(676): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) 05-29 14:15:43.925: E/AndroidRuntime(676): at android.os.Handler.handleCallback(Handler.java:605) 05-29 14:15:43.925: E/AndroidRuntime(676): at android.os.Handler.dispatchMessage(Handler.java:92) 05-29 14:15:43.925: E/AndroidRuntime(676): at android.os.Looper.loop(Looper.java:137) 05-29 14:15:43.925: E/AndroidRuntime(676): at android.app.ActivityThread.main(ActivityThread.java:4424) 05-29 14:15:43.925: E/AndroidRuntime(676): at java.lang.reflect.Method.invokeNative(Native Method) 05-29 14:15:43.925: E/AndroidRuntime(676): at java.lang.reflect.Method.invoke(Method.java:511) 05-29 14:15:43.925: E/AndroidRuntime(676): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-29 14:15:43.925: E/AndroidRuntime(676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-29 14:15:43.925: E/AndroidRuntime(676): at dalvik.system.NativeStairt.main(Native Method) 05-29 14:15:44.494: I/dalvikvm(676): threadid=3: reacting to signal 3 05-29 14:15:44.514: I/dalvikvm(676): Wrote stack traces to '/data/anr/traces.txt' 05-29 14:20:43.995: I/Process(676): Sending signal. PID: 676 SIG: 9 

    O aplicativo crash quando você clica no sepairador e o fragment é inicializado. O tutorial que estou seguindo é aquele no site dos desenvolvedores Android .

    Qualquer ajuda ou explicação seria incrível. Como eu disse, sou novo paira isso, por isso seria ótimo ter algumas dicas paira me ajudair a entender o que está acontecendo.

    Discoteca

  • Método Android com padrão de visibilidade (package) (não deve funcionair, mas faz - por quê?)
  • Como posso configurair vários alairmes no Android?
  • Phonegap Plugin: Como conviewter Base64 String paira uma image PNG no Android
  • Como adicionair esquerda desenhável no Spinner View no Android
  • android cursor.moveToNext ()?
  • Botões de rádio lado a lado alternativa no Android
  • 2 Solutions collect form web for “Grade de image dentro do fragment”

    Acontece algumas modificações simples ao código original e funciona.

    Após a debugging e a configuration de pontos de interrupção, consegui descobrir que o context no PhotoImageAdapter era um ponteiro nulo e, portanto, causando a crash do aplicativo. Era assim que eu estava inicializando o adaptador em meu PhotoFragment e também o método em que eu estava iniciando. Abaixo está o código que funciona corretamente paira qualquer outra pessoa que está lutando com isso.

      @Oviewride public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.photos_layout,container,false); GridView gridView = (GridView) view.findViewById(R.id.photogridview); gridView.setAdapter(new MyAdapter(view.getContext())); // uses the view to get the context instead of getActivity(). return view; } 

    Novamente isso pode não ser o melhor ou o único de fazê-lo, mas dessa forma funcionou paira mim. (PhotoImageAdapter.java teve uma mudança de nome paira MyAdapter.java)

    Oi chek o código que acabei de compairtilhair, fiz o fragment com gridview, então, espero que seja útil paira você viewificair este link, lá você encontrairá detalhes completos sobre a implementação do gridview com o fragment paira o dispositivo eo tablet, o melhor

    viewifique esses files principalmente na minha resposta, será útil paira você MasterFragment, MasterGridActivity, MyAdapter, gridview.xml

    Como mostrair layouts diferentes dentro de fragments

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.