Reputation: 225
what do i have is many tabs between fragments .. each fragment has grid view and each time we press an image in the grid view it should be displayed full screen in a new activity .. and i want to add swipe functionality in this full screen image so the user can swipe left and right between other images in the grid view ... but i keep see an error on the full screen activity and log gave NULL POINTER EXCEPTION !!! please help ..
first of all here is the code for one of the fragments class :
public class KitchenBlinds extends Fragment {
public Integer[] mThumbIds = {
R.drawable.kit_1, R.drawable.kit_2,
R.drawable.kit_3, R.drawable.kit_4,
R.drawable.kit_5, R.drawable.kit_6,
R.drawable.kit_7, R.drawable.kit_8,
R.drawable.kit_9, R.drawable.kit_10,
R.drawable.kit_11, R.drawable.kit_12,
R.drawable.kit_13, R.drawable.kit_14,
R.drawable.kit_15
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_kitchen_blinds, container, false);
GridView gridview = (GridView)view.findViewById(R.id.grid_view);
try{
// Instance of ImageAdapter Class
gridview.setAdapter(new ImageAdapter(getActivity(), mThumbIds));
} catch (OutOfMemoryError E) {
E.printStackTrace();
}
/**
* On Click event for Single Gridview Item
* */
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(), FullImageActivity2.class);
// passing array index
i.putExtra("id", mThumbIds[position]);
Log.d("ID", "" + mThumbIds[position]);
startActivity(i);
}
});
return view;
}
}
and then here is the code of the image adapter for the grid view class :
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {};
public ImageAdapter(Context c,Integer[] mThumbIds2){
mContext = c;
this.mThumbIds=mThumbIds2;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setBackgroundResource(R.layout.edit_border);
imageView.setPadding(3, 3, 3, 3);
return imageView;
}
}
and here is the code for the Image Pager Adapter :
public class ImagePagerAdapter extends PagerAdapter{
private List<ImageView> images;
public ImagePagerAdapter(List<ImageView> images) {
this.images = images;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = images.get(position);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}
@Override
public int getCount() {
return images.size();
}
@Override
public boolean isViewFromObject(View view, Object o) {
// TODO Auto-generated method stub
return view == o;
}
}
and finally here is the code of the full screen activity that it crash on it each time i select the image :
public class FullImageActivity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image_activity2);
// Loop through the ids to create a list of full screen image views
ImageAdapter imageAdapter = new ImageAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
images.add(imageView);
}
// Finally create the adapter
ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(images);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(imagePagerAdapter);
// Set the ViewPager to point to the selected image from the previous activity
// Selected image id
int position = getIntent().getExtras().getInt("id");
viewPager.setCurrentItem(position);
}
}
and i keep see an error on this line :
ImageAdapter imageAdapter = new ImageAdapter(this);
because i configure my constructor in the image adapter to take two parameter which work fine , but i don't know how to pass it here !!! please help !! and thx in advance ...
Upvotes: 1
Views: 1464
Reputation: 225
well .. i fix the problem by sending the array in the kitchen blinds activity like this :
i.putExtra("array", mThumbIds);
then i receive it like this and add it to the constructor :
Intent i = getIntent();
Object[] s = (Object[]) getIntent().getSerializableExtra("array");
Integer[] newArray = Arrays.copyOf(s, s.length, Integer[].class);
and add it to the constructor :
ImageAdapter imageAdapter = new ImageAdapter(this, newArray);
Upvotes: 1