Image GridView Inside Fragment

I just started to develop on the Android platform after developing on iOS. I looked around and I can’t figure it out. I am trying to get a view of the grid after selecting a tab in the action bar. The snippet is displayed as the main action that controls the tab bar. I think the problem may be with context passing, but I'm not sure.

Here is my MainActivity.java . Here the fragment is initialized and attached to the activity. It works without code in the fragment.

if (mFragment == null){ mFragment = Fragment.instantiate(mActivity, mClass.getName()); ft.add(android.R.id.content,mFragment,mTag); } else { ft.attach(mFragment); } 

Here are my PhotosFragment.java. Here I want the grid view to be filled and displayed.

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

Here is my class PhotoImageAdapter.java . This is where the images are added to the adapter, I think.

  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 convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } 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 }; } 

And here is my photos_layout which contains a gridview with id photogridview. photos_layout.xml

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

EDIT

Here is a log report when it crashes

  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.NativeStart.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 

When you click on the tab, the application will work and the fragment will be initialized. The next tutorial is what is on the Android developer sites .

Any help or explanation would be awesome. As I said, I'm new to this, so it would be great to have some pointers to help me understand what is going on.

Disco

+7
source share
2 answers

It turns out some simple modifications to the source code, and it works.

After debugging and setting breakpoints, I was able to find that the context in the PhotoImageAdapter was a null pointer and therefore caused the application to crash. It was a way to initialize the adapter in my PhotoFragment, as well as the method in which I initialized it. Below is the code that works correctly for anyone struggling with this.

  @Override 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; } 

Again, this may not be the best or the only one, but this method worked for me. (PhotoImageAdapter.java changed the name to MyAdapter.java)

+20
source

Hi, the code that I just passed, I made a fragment with a gridview, so I hope it will be useful for you to check this link, there you will find full information about the implementation of gridview with a fragment for both the device and tablets.

check these files mainly in my answer, it will definitely help MasterFragment, MasterGridActivity, MyAdapter, gridview.xml

How to show different layouts inside fragments

+1
source

All Articles