Android アプリケーションのフラグメントに gridview を実装しようとすると、getApplicationContext()エラーとgetSystemServiceエラーが発生します。これを解決するにはどうすればよいですか? ソース コードにエラーはありますか?
`
private GridView photoGrid;
private int mPhotoSize, mPhotoSpacing;
private ImageAdapter imageAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
// get the photo size and spacing
mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);
// initialize image adapter
imageAdapter = new ImageAdapter();
photoGrid = (GridView) getView().findViewById(R.id.albumGrid);
photoGrid.setOnItemClickListener(this);
GridView gridview = (GridView) getView().findViewById (R.id.albumGrid);
gridview.setAdapter (new ImageAdapter());
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
Intent myIntent = null;
if(position == 0){
myIntent = new Intent(v.getContext(), PregnancyStagesMainActivity.class);
}
if(position == 1){
myIntent = new Intent(v.getContext(), ComplicationsMainActivity.class);
}
if(position == 2){
myIntent = new Intent(v.getContext(), DietAndFitnessMainActivity.class);
}
if(position == 3){
myIntent = new Intent(v.getContext(), MythsAndFactsMainActivity.class);
}
if(position == 4){
myIntent = new Intent(v.getContext(), HelplinesMainActivity.class);
}
if(position == 5){
myIntent = new Intent(v.getContext(), FaqsMainActivity.class);
}
startActivity(myIntent);
}
});
// set image adapter to the GridView
photoGrid.setAdapter(imageAdapter);
// Set the Required Animation to GridView and start the Animation
// use fly_in_from_center to have 2nd type of animation effect (snapshot 2)
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.in_from_left);
gridview.setAnimation(anim);
anim.start();
// get the view tree observer of the grid and set the height and numcols dynamically
photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (imageAdapter.getNumColumns() == 0) {
final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
if (numColumns > 0) {
final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
imageAdapter.setNumColumns(numColumns);
imageAdapter.setItemHeight(columnWidth);
}
}
}
});
}
// ///////// ImageAdapter class /////////////////
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private int mItemHeight = 0;
private int mNumColumns = 0;
private RelativeLayout.LayoutParams mImageViewLayoutParams;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
}
public int getCount() {
return CONTENT.length;
}
// set numcols
public void setNumColumns(int numColumns) {
mNumColumns = numColumns;
}
public int getNumColumns() {
return mNumColumns;
}
// set photo item height
public void setItemHeight(int height) {
if (height == mItemHeight) {
return;
}
mItemHeight = height;
mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
notifyDataSetChanged();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
if (view == null)
view = mInflater.inflate(R.layout.photo_item, null);
ImageView cover = (ImageView) view.findViewById(R.id.cover);
TextView title = (TextView) view.findViewById(R.id.title);
cover.setLayoutParams(mImageViewLayoutParams);
// Check the height matches our calculated column width
if (cover.getLayoutParams().height != mItemHeight) {
cover.setLayoutParams(mImageViewLayoutParams);
}
cover.setImageResource(ICONS[position % ICONS.length]);
title.setText(CONTENT[position % CONTENT.length]);
return view;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
} `