1

I'm having a little bit of trouble on implementing a gallery into a Fragment. The gallery I'm trying to put in used to be an Activity but I want it to now be a Fragment since I'm now working on a different project to make my app look more cleaner. I'm getting an error at

gridView.setAdapter(new ImageAdapter(this));

and at

 Intent i = new Intent(getApplicationContext(), FullImageActivity.class);

I honestly have no idea on how to fix these any help would be awesome sauce

Also, I don't have much experience with Fragments and I'm quite new to it so any details that could help me understand what I'm doing wrong is welcomed!

Here's the Java code:

package info.androidhive.slidingmenu;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;


public class GalleryActivity extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); }
    //setContentView(R.layout.gallery_layout);

    public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
            Bundle saedInstanceState) {
        View rootView = inflater.inflate(R.layout.gallery_layout, parent, false);
    GridView gridView = (GridView) rootView.findViewById(R.id.photos);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
            // passing array index
            i.putExtra("id", position);
            startActivity(i);
        }
    });
}
}
Kodi
  • 109
  • 1
  • 2
  • 12

3 Answers3

3

Change this

 gridView.setAdapter(new ImageAdapter(this));

to

 gridView.setAdapter(new ImageAdapter(getActivity()));

this does not refer to Activity Context since your class extends Fragment. getActvity() returns the activity this fragment is associated with.

Also change

  Intent i = new Intent(getApplicationContext(), FullImageActivity.class);

to

  Intent i = new Intent(getActivity(), FullImageActivity.class);

Also you need to return rootView at the end of onCreateView

@Override // missing annotation
public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
        Bundle saedInstanceState) {
    View rootView = inflater.inflate(R.layout.gallery_layout, parent, false);
     ...// rest of the code

     return rootView;
}

Or Override onActivityCreated and use getView to initialize gridview. just return view in onCreateView

 @Override // missing annotation
 public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
        Bundle saedInstanceState) {
    View rootView = inflater.inflate(R.layout.gallery_layout, parent, false);

     return rootView;
 }

Then

@Override
public void onActivityCreated(Bundle savedInstanceState) {

 GridView gridView = (GridView) getView().findViewById(R.id.photos);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(getActivity()));
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getActivity(), FullImageActivity.class);
            // passing array index
            i.putExtra("id", position);
            startActivity(i);
        }
    });
   }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • This code seems to work as I see no errors being shown in Eclipse which is a super good thing because I had no idea how to get rid of them but now when I launch the app and go into the Gallery section it Force Closes on me. Could it possibly be my image-adapter? I have everything in the Manifest so I don't think there's anything wrong there. What would you suppose it is? – Kodi Dec 25 '13 at 18:35
  • @Kodi with the code posted this all i can post and i have posted. For a new problem post the stack trace and then i can tell which part of the code causes problem. Actually to the code posted. this is the answer – Raghunandan Dec 25 '13 at 18:36
  • @Kodi possibly something might be wrong in ImageAdapter. post the logcat and the relevant code and post a new question – Raghunandan Dec 25 '13 at 18:38
  • thank you for your help! You did answer my question and I really appreciate it. I did as you said and posted a new question here http://stackoverflow.com/questions/20775968/gallery-force-close-using-fragment – Kodi Dec 25 '13 at 19:14
0

Try this below code

  @Override     
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {      
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.gallery_layout, null);
            GridView gv1=(GridView)root.findViewById(R.id.gridview);
            gv1.setAdapter(new ImageAdapter(getActivity()));
     gridView.setAdapter(new ImageAdapter(this));
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });
            return root;
        }
Sanket990
  • 665
  • 1
  • 10
  • 22
0
      @Override// This must be in above in your fragment implementing class
   public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
        Bundle saedInstanceState) {
 View rootView = inflater.inflate(R.layout.gallery_layout, parent, false);
GridView gridView = (GridView) rootView.findViewById(R.id.photos);

// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(getActivity()));
gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long id) {

        // Sending image id to FullScreenActivity
        Intent i = new Intent(getActivity(), FullImageActivity.class);
        // passing array index
        i.putExtra("id", position);
        startActivity(i);
       }
      } 
    );
    }
  return rootView;
 }

Remove this from your code :

    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); }
   //setContentView(R.layout.gallery_layout);

This is also not a good approach you should put your fragment code after activity has been created.

Inside this method:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
rupesh
  • 2,865
  • 4
  • 24
  • 50
  • onActivityCreated will get call only when activity has been created. So this is kind of assurance that your activity has been created if you are passing any thing form activity. Now you can create your fragment. – rupesh Dec 25 '13 at 05:31
  • but i see nothing wrong with having `onCreate`. Atleast not in the code posted by op. So why remove it? – Raghunandan Dec 25 '13 at 06:16
  • then there is no need to remove `onCreate` although removing or keeping it won't make any difference with the op code. – Raghunandan Dec 25 '13 at 06:53
  • @Raghunandan can u please have a look in this question reply me over there. thanks in Advance http://stackoverflow.com/questions/20748126/is-there-anyway-to-set-the-no-of-row-in-gridview-android – rupesh Dec 25 '13 at 06:56
  • its already answered use `Tablelayout`. With gridView you can specify only the number of columns. You can't specify rows. – Raghunandan Dec 25 '13 at 06:57
  • i want to display my List of product is like this http://www.androidviews.net/wp-content/uploads/2012/10/device-2012-10-24-002112.png – rupesh Dec 25 '13 at 06:59