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 6826443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:08:53+00:00 2026-05-26T22:08:53+00:00

I have a GridView with pictures/text, loaded from different servers (I only know the

  • 0

I have a GridView with pictures/text, loaded from different servers (I only know the URL).
I tried to modify my code, based on the tutorial of ryac.

In my Activity file, I set the GAdapter to my GridView like this:

GridView mGridMain = (GridView)findViewById(R.id.gvMain);
mGridMain.setAdapter(new GAdapter(this, listAppInfo));

I have modified my own adapter and try to adapt it:

public class GAdapter extends BaseAdapter {

 private Context mContext;
 private List<SiteStaff> mListAppInfo;
 private HashMap<Integer, ImageView> views;

 /**
  * @param context
  * @param list
  */
 public GAdapter(Context context, List<SiteStaff> list) {
  mContext = context;
  mListAppInfo = list;  
 }

 @Override
 public int getCount() {
  return mListAppInfo.size();
 }

 @Override
 public Object getItem(int position) {
  return mListAppInfo.get(position);
 }

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

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {

  SiteStaff entry = mListAppInfo.get(position);

  if(convertView == null) {
   LayoutInflater inflater = LayoutInflater.from(mContext);
   convertView = inflater.inflate(R.layout.list_g, null);
  }

  ImageView v;

  // get the ImageView for this position in the GridView..
  v = (ImageView)convertView.findViewById(R.id.ivIcon);

  //set default image
  v.setImageResource(android.R.drawable.ic_menu_gallery);

  TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
  tvName.setText(entry.getName());


  Bundle b = new Bundle ();
  b.putString("file", "http://www.test.com/mypict.jpg");
  b.putInt("pos", position);

  // this executes a new thread, passing along the file
  // to load and the position via the Bundle..
  new LoadImage().execute(b);

  // puts this new ImageView and position in the HashMap.
  views.put(position, v);


  // return the view to the GridView..
  // at this point, the ImageView is only displaying the
  // default icon..
  return v;

 }




 // this is the class that handles the loading of images from the cloud
 // inside another thread, separate from the main UI thread..
 private class LoadImage extends AsyncTask<Bundle, Void, Bundle> {

  @Override
  protected Bundle doInBackground(Bundle... b) {

   // get the file that was passed from the bundle..
   String file = b[0].getString("file");

   URL UrlImage;
   Bitmap bm=null;
   try {
    UrlImage = new URL (file);
    HttpURLConnection connection;
    connection = (HttpURLConnection) UrlImage.openConnection(); 
    bm= BitmapFactory.decodeStream(connection.getInputStream());

   } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }   


   // now that we have the bitmap (bm), we'll
   // create another bundle to pass to 'onPostExecute'.
   // this is the method that is called at the end of 
   // our task. like a callback function..
   // this time, we're not passing the filename to this
   // method, but the actual bitmap, not forgetting to
   // pass the same position along..

   Bundle bundle = new Bundle();
   bundle.putParcelable("bm", bm);
   bundle.putInt("pos", b[0].getInt("pos"));
   bundle.putString("file", file); // this is only used for testing..

   return bundle;
  }

  @Override
  protected void onPostExecute(Bundle result) {
   super.onPostExecute(result);

   // just a test to make sure that the position and
   // file name are matching before and after the
   // image has loaded..
   Log.d("test", "*after: " + result.getInt("pos") + " | " + result.getString("file"));

   // here's where the photo gets put into the
   // appropriate ImageView. we're retrieving the
   // ImageView from the HashMap according to
   // the position..
   ImageView view = views.get(result.getInt("pos"));

   // then we set the bitmap into that view. and that's it.
   view.setImageBitmap((Bitmap) result.getParcelable("bm"));
  }

 }

}

But the below error is displayed:

11-16 06:16:08.285: E/AndroidRuntime(517): FATAL EXCEPTION: main
11-16 06:16:08.285: E/AndroidRuntime(517): java.lang.NullPointerException
11-16 06:16:08.285: E/AndroidRuntime(517):  at common.adapter.GAdapter.getView(GAdapter.java:139)
11-16 06:16:08.285: E/AndroidRuntime(517):  at android.widget.AbsListView.obtainView(AbsListView.java:1315)
11-16 06:16:08.285: E/AndroidRuntime(517):  at android.widget.GridView.onMeasure(GridView.java:932)
11-16 06:16:08.285: E/AndroidRuntime(517):  at android.view.View.measure(View.java:8171)
11-16 06:16:08.285: E/AndroidRuntime(517):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
11-16 06:16:08.285: E/AndroidRuntime(517):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
11-16 06:16:08.285: E/AndroidRuntime(517):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
11-16 06:16:08.285: E/AndroidRuntime(517):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
11-16 06:16:08.285: E/AndroidRuntime(517):  at android.view.View.measure(View.java:8171)
11-16 06:16:08.285: E/AndroidRuntime(517):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
11-16 06:16:08.285: E/AndroidRuntime(517):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-16 06:16:08.285: E/AndroidRuntime(517):  at android.view.View.measure(View.java:8171)

Until now, I never used Threads in Android. I would be grateful if someone can help me.

  • 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-05-26T22:08:53+00:00Added an answer on May 26, 2026 at 10:08 pm

    I fount in getView() at the end you are doing

    views.put(position, v);
    

    But it views never initialized. That’s why you are getting java.lang.NullPointerException

    To avoid this please initialized hash map object in your in GAdapter Constructor

    public GAdapter(Context context, List<SiteStaff> list) {
      mContext = context;
      mListAppInfo = list;  
      views = new HashMap<Integer, ImageView>();
    
     }
    

    If you check the example you are refering in your question you will find

    public ImageAdapter (Context c, String f, ArrayList<HashMap<String, Object>> p) {
            ...
            views = new HashMap<Integer, ImageView>();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview with pictures of equipment to select from. Once selected, the
I have gridview binded from database.. I have following code: protected void Page_Load(object sender,
I have a GridView populated from an ObjectDataSource with two items in its DataKeyNames
I have stored a list of items from gridView into the registry as below:
In my aspx page I have a gridview which displays the value from my
I have GridView that allows select. It takes data from EntityDataSource . How do
I have GridView with AutoGenerateDeleteButton=true && AutoGenerateEditButton=true . I want to allow only registered
Hello I have a gridView which I use to show some pictures on (small
Well, I have GridView with editable field like 'TemplateField' with DropDownList. My code: <Columns>
I have Gridview like this. Here is my last column Gridview code; <EditItemTemplate> <asp:TextBox

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.