Eclipse で Android ギャラリーを作成しているときに、このエラーが発生します。画像を 2 つだけ追加すると正常に動作しますが、ギャラリーにさらに画像を追加すると、このエラーが発生します。どんな助けでも大歓迎です!
package com.hospital;
import com.hospital.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class myGallery extends Activity {
private final Integer[] pic = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3
};
ImageView imageview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery gallery = (Gallery)findViewById(R.id.pics);
gallery.setAdapter(new ImageAdapter(this));
imageview = (ImageView)findViewById(R.id.ImageView01);
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView <?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(myGallery.this, "" + arg0, Toast.LENGTH_SHORT).show();
imageview.setImageResource(pic[arg2]);
}
});
registerForContextMenu(gallery);
}
public class ImageAdapter extends BaseAdapter {
private final int mGalleryItemBackground;
private final Context mContext;
private final Integer[] pic = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = c.obtainStyledAttributes(R.styleable.photoGallery);
mGalleryItemBackground = attr.getResourceId(
R.styleable.photoGallery_android_galleryItemBackground, 1);
attr.recycle();
}
public int getCount() {
return pic.length;
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(pic[arg0]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
}