0

基本的に、描画可能なリソースからの一連の画像を表示するグリッド ビューがありましたが、代わりに携帯電話の SD カードから画像を取得したいと考えています。

これに対してヌルポインタ例外が発生しています: return cursor.getCount(); これも: gridview.setAdapter(new ImageAdapter(this));

nullの理由を説明できる人はいますか?カーソルを初期化しようとしましたが、それにも問題がありました。

public class MainActivity extends Activity {
ImageAdapter adapter;
private Cursor cursor;
private  int columnIndex;

@Override
public void onCreate(Bundle savedInstanceState) {
    View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] projection = {MediaStore.Images.Thumbnails._ID};
    cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Thumbnails._ID);
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView gridview = (GridView)view.findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));
    adapter.notifyDataSetChanged();

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            String[] projection = new String[]{MediaStore.Images.Media.DATA};
            cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Thumbnails._ID);
            columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
            cursor.moveToPosition(position);
            String imagePath = cursor.getString(columnIndex);
            //Toast below states position of image
            //Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();

            //Sending image id to fullScreenActivity
            Intent i = new Intent(getApplicationContext(), FullImageActivity.class);

            //passing array index
            i.putExtra("id", position);
            startActivity(i);
        }
    });
}

public void startCamera(View v){
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, 0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}




public class ImageAdapter extends BaseAdapter {
Context mContext;
ImageView imageview;
Cursor cursor;
int columnIndex;

public ImageAdapter(Context adapter) {
    mContext = adapter;
}

public int getCount() {
    return cursor.getCount();
    //return mThumbIds.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        cursor.moveToPosition(position);
        int imageID = cursor.getInt(columnIndex);
        imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));

        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    //imageView.setImageResource(mThumbIds[position]);
    //imageView.setImageDrawable(mySDCardImages.get(position).getDrawable());
    return imageView;
}

        // references to our images
        /* public 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
};*/

}


      public class FullImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    //get intent data
    Intent i = getIntent();
    Cursor cursor = null;

    //selected image id
    int position = i.getExtras().getInt("id");
    MainActivity myActivity = new MainActivity();
    ImageAdapter imageAdapter = new ImageAdapter(this, cursor);

    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(position);
}

      }
4

1 に答える 1

0

値が設定されていないアダプター クラスにカーソルがあるため、常に null になります。宣言を削除し、親のカーソルを使用します。

于 2012-11-15T18:09:36.507 に答える