Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get a string array from an async task into an image adapter?
    primarykey
    data
    text
    <p>I am trying to adapt the google example BitmapFun of processing bitmaps into my own code. I get the picture urls from a mysql database via JSON through an async task (SearchDaMovies). The problem is, I can't get the pictures to load in the image adapter. It works with the example array "imageThumbUrls". But when I replace the "imageThumbUrls" string array with the string array I need, "arrayS", it gives me a NullPointerException. How should I do this? I've been stuck on this for a week.</p> <pre><code>public class ImageGridFragment extends Fragment implements AdapterView.OnItemClickListener { private static final String TAG = "ImageGridFragment"; private static final String IMAGE_CACHE_DIR = "thumbs"; private int mImageThumbSize; private int mImageThumbSpacing; private ImageAdapter mAdapter; private ImageFetcher mImageFetcher; String id, name, year, genre, known, tag1, tag2, tag3, poster, oster, description, vote ; private ProgressDialog pDialog; jParser parser = new jParser(); ArrayList&lt;HashMap&lt;String, String&gt;&gt; movies; private static final String urlGetPid = "http://linkpictures.com"; private static final String TAG_SUCCESS = "success"; JSONArray jArray = null; private static final String TAG_PID = "pid"; private static final String TAG_POSTER = "poster"; int success; /** * Empty constructor as per the Fragment documentation . */ public ImageGridFragment() {} @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mImageThumbSize = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size); mImageThumbSpacing = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_spacing); mAdapter = new ImageAdapter(getActivity()); ImageCacheParams cacheParams = new ImageCacheParams(getActivity(), IMAGE_CACHE_DIR); cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory // The ImageFetcher takes care of loading images into our ImageView children asynchronously mImageFetcher = new ImageFetcher(getActivity(), mImageThumbSize); mImageFetcher.setLoadingImage(R.drawable.empty_photo); mImageFetcher.addImageCache(getActivity().getSupportFragmentManager(), cacheParams); } @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { movies = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); new SearchDaMovies().execute(); final View v = inflater.inflate(R.layout.activity_show_movies, container, false); final ListView mGridView = (ListView) v.findViewById(R.id.listView1); Bundle extras = getActivity().getIntent().getExtras(); name = extras.getString("name"); year = extras.getString("year"); known = extras.getString("known"); genre = extras.getString("genre"); tag1 = extras.getString("tag1"); tag2 = extras.getString("tag2"); tag3 = extras.getString("tag3"); vote = extras.getString("vote"); description = extras.getString("description"); mGridView.setAdapter(mAdapter); mGridView.setOnItemClickListener(this); mGridView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int scrollState) { // Pause fetcher to ensure smoother scrolling when flinging if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) { mImageFetcher.setPauseWork(true); } else { mImageFetcher.setPauseWork(false); } } @Override public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }); return v; } @Override public void onResume() { super.onResume(); mImageFetcher.setExitTasksEarly(false); mAdapter.notifyDataSetChanged(); } @Override public void onPause() { super.onPause(); mImageFetcher.setPauseWork(false); mImageFetcher.setExitTasksEarly(true); mImageFetcher.flushCache(); } @Override public void onDestroy() { super.onDestroy(); mImageFetcher.closeCache(); } @TargetApi(16) @Override public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) { if (Utils.hasJellyBean()) { ActivityOptions options = ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getWidth(), `v.getHeight());` } else { } } public class SearchDaMovies extends AsyncTask&lt;String, String, String[]&gt;{ String[] arrayS; @Override protected String[] doInBackground(String... args) { try { // send the query to JSON List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); params.add(new BasicNameValuePair("name", name)); params.add(new BasicNameValuePair("year", year)); params.add(new BasicNameValuePair("genre", genre)); params.add(new BasicNameValuePair("known", known)); params.add(new BasicNameValuePair("tag1", tag1)); params.add(new BasicNameValuePair("tag2", tag2)); params.add(new BasicNameValuePair("tag3", tag3)); params.add(new BasicNameValuePair("vote", vote)); params.add(new BasicNameValuePair("poster", poster)); params.add(new BasicNameValuePair("description", description)); //receive the JSON data JSONObject json = parser.makeHttpRequest(urlGetPid, params); success = json.getInt(TAG_SUCCESS); if (success == 1){ jArray = json.getJSONArray("Table"); for (int i =0; i&lt;jArray.length();i++){ JSONObject c = jArray.getJSONObject(i); String id = c.getString("pid"); String ame = c.getString("name"); String ear = c.getString("year"); String enre = c.getString("genre"); String nown = c.getString("known"); String ag1 = c.getString("tag1"); String ote = c.getString("vote"); if (ote.length()&gt;14){ ote = ote.substring(0, ote.length() - 14); } oster = c.getString("poster"); //get the posters in an array arrayS= new String[100]; arrayS[i]=oster; Log.d("weird array ", arrayS[i]); String escription = c.getString("description"); HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put("pid", id); map.put("name", ame); map.put("year", ear); map.put("genre", enre); map.put("known",nown); map.put("tag1", ag1); map.put("vote", ote); map.put("poster", oster); map.put("description", escription); movies.add(map); } } } catch (JSONException e) { e.printStackTrace(); } return arrayS; } } /** * The main adapter that backs the GridView. This is fairly standard except the number of * columns in the GridView is used to create a fake top row of empty views as we use a * transparent ActionBar and don't want the real top row of images to start off covered by it. */ public final static String[] imageThumbUrls = new String[] { "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s160-c/A%252520Photographer.jpg", "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s160-c/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg", "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s160-c/Another%252520Rockaway%252520Sunset.jpg", "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s160-c/Antelope%252520Butte.jpg", "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s160-c/Antelope%252520Hallway.jpg", "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s160-c/Antelope%252520Walls.jpg", }; private class ImageAdapter extends BaseAdapter { private final Context mContext; private int mItemHeight = 0; private int mNumColumns = 0; private int mActionBarHeight = 0; private GridView.LayoutParams mImageViewLayoutParams; // private ArrayList&lt;HashMap&lt;String, String&gt;&gt; movies = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); public ImageAdapter(Context context) { super(); mContext = context; mImageViewLayoutParams = new GridView.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // Calculate ActionBar height TypedValue tv = new TypedValue(); if (context.getTheme().resolveAttribute( android.R.attr.actionBarSize, tv, true)) { mActionBarHeight = TypedValue.complexToDimensionPixelSize( tv.data, context.getResources().getDisplayMetrics()); } } @Override public int getCount() { // Size + number of columns for top empty row return imageThumbUrls.length; } @Override public Object getItem(int position) { return position &lt; mNumColumns ? null : imageThumbUrls[position]; } @Override public long getItemId(int position) { return position &lt; mNumColumns ? 0 : position - mNumColumns; } @Override public int getViewTypeCount() { // Two types of views, the normal ImageView and the top row of empty views return 2; } @Override public int getItemViewType(int position) { return (position &lt; mNumColumns) ? 1 : 0; } @Override public boolean hasStableIds() { return true; } @Override public View getView(int position, View convertView, ViewGroup container) { // First check if this is the top row if (position &lt; mNumColumns) { if (convertView == null) { convertView = new View(mContext); } // Set empty view with height of ActionBar convertView.setLayoutParams(new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight)); return convertView; } // Now handle the main ImageView thumbnails ImageView imageView; if (convertView == null) { // if it's not recycled, instantiate and initialize imageView = new RecyclingImageView(mContext); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(mImageViewLayoutParams); } else { // Otherwise re-use the converted view imageView = (ImageView) convertView; } // Check the height matches our calculated column width if (imageView.getLayoutParams().height != mItemHeight) { imageView.setLayoutParams(mImageViewLayoutParams); } // Finally load the image asynchronously into the ImageView, this also takes care of // setting a placeholder image while the background thread runs mImageFetcher.loadImage(imageThumbUrls[position], imageView); return imageView; } // * Sets the item height. Useful for when we know the column width so the height can be set // * to match. // // * @param height public void setItemHeight(int height) { if (height == mItemHeight) { return; } mItemHeight = height; mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight); mImageFetcher.setImageSize(height); notifyDataSetChanged(); } public void setNumColumns(int numColumns) { mNumColumns = numColumns; } public int getNumColumns() { return mNumColumns; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload