Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8845411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:44:00+00:00 2026-06-14T11:44:00+00:00

Basically I had a grid view that showed a set of images from the

  • 0

Basically I had a grid view that showed a set of images from the drawable resources but I want it to pull up images from my phone’s sd card instead.

I’m getting a null pointer exception for this: return cursor.getCount();
and this also: gridview.setAdapter(new ImageAdapter(this));

Could anybody explain why its null? I tried to initialise cursor but I was having trouble with that too.

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);
}

      }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:44:02+00:00Added an answer on June 14, 2026 at 11:44 am

    You have a cursor in your adaptor class that never has its value set, so will always be null. Delete the declaration and use the parent’s cursor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So basically the assignment was we had to create a doubly linked list that's
I basically have a simple problem in my program that I just want to
Basically, I have a view that needs to be split up into a 2x4
Basically, I had a project that was written with Visual Studio 2010 Express in
I have had lots of help with creating a PHP script that basically grabs
So basically I had to switch from 1.5 to 1.6 to add in Mobfox.
So basically we released a Recovery image that had a file deleted that was
Basically if I had a mysql table with columns: id, type, content, version. There
Im basically a php programmer and eventually i had to work on a java
Well basically, one of my mates was practicing JS and he had an idea

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.