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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:03:55+00:00 2026-06-17T02:03:55+00:00

My app is using GridView to get image from JSON. Everything is ok but

  • 0

My app is using GridView to get image from JSON. Everything is ok but now I want to make it for loading more by clicking on button. I followed this tutorial Android ListView with Load More Button.
When I followed it, I used runOnUiThread(new Runnable() {
public void run() { } }
in the doInBackground but it errors NetworkOnMainThreadException i knew this cuz I run it on UI thread. but when I remove runOnUiThread..., it errors also

How can I fix this error? full source http://pastie.org/5659969

01-10 14:39:14.970: E/AndroidRuntime(19609): FATAL EXCEPTION: AsyncTask #3
01-10 14:39:14.970: E/AndroidRuntime(19609): java.lang.RuntimeException: An error occured while executing doInBackground()
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at java.lang.Thread.run(Thread.java:856)
01-10 14:39:14.970: E/AndroidRuntime(19609): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4847)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:974)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.view.ViewGroup.invalidateChild(ViewGroup.java:4075)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.view.View.invalidate(View.java:10525)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.view.View.invalidate(View.java:10480)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.widget.ImageView.invalidateDrawable(ImageView.java:190)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:350)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.graphics.drawable.Drawable.setVisible(Drawable.java:546)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.widget.ImageView.onDetachedFromWindow(ImageView.java:1173)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.view.View.dispatchDetachedFromWindow(View.java:12096)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2541)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:3828)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.widget.AbsListView.resetList(AbsListView.java:2130)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.widget.GridView.setAdapter(GridView.java:170)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at com.camitss.mcolle.CardActivity$LoadMore.doInBackground(CardActivity.java:254)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at com.camitss.mcolle.CardActivity$LoadMore.doInBackground(CardActivity.java:1)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-10 14:39:14.970: E/AndroidRuntime(19609):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-10 14:39:14.970: E/AndroidRuntime(19609):    ... 5 more

AsyncTask when first load

// Download JSON in Background
public class DownloadJSONFileAsync extends AsyncTask<String, Void, Void> {
    String token = getIntent().getExtras().getString("token1");

    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
    }

    @Override
    protected Void doInBackground(String... params) {
        String url = "http://xxx.xxx.xxx/card/all/"+num_page+"/0/?token="+token;
        JSONArray data = null;

        try {

            JSONObject jsonObject = new JSONObject(getJSONUrl(url));
            MyArrList = new ArrayList<HashMap<String, Object>>();
            HashMap<String, Object> map;
            data = jsonObject.getJSONArray("data");

            for (int i = 0; i < data.length(); i++) {
                JSONObject c = data.getJSONObject(i);
                map = new HashMap<String, Object>();

                // Thumbnail Get ImageBitmap To Object
                map.put("cardimage", (String) c.getString("cardimage"));
                map.put("ImageThumBitmap",(Bitmap) loadBitmap(c.getString("cardimage")));
                map.put("categoryid", (String) c.getString("categoryid"));
                MyArrList.add(map);
            }

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

        return null;
    }

    protected void onPostExecute(Void unused) {
        ShowAllContent(); // When Finish Show Content
        dismissDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
        removeDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
    }

}

AsycTask when using button to click to load more

// load more

public class LoadMore extends AsyncTask<String, Void, Void> {

    String token = getIntent().getExtras().getString("token1");

    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
    }

    @Override
    protected Void doInBackground(String... params) {
                num_page += 1;
                String url = "http://xxx.xxx.xxx/card/all/"+num_page+"/0/?token="+token;
                JSONArray data = null;

                try {

                    JSONObject jsonObject = new JSONObject(getJSONUrl(url));

                    MyArrList = new ArrayList<HashMap<String, Object>>();
                    HashMap<String, Object> map;
                    data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject c = data.getJSONObject(i);
                        map = new HashMap<String, Object>();

                        // Thumbnail Get ImageBitmap To Object
                        map.put("cardimage", (String) c.getString("cardimage"));
                        map.put("ImageThumBitmap",(Bitmap) loadBitmap(c.getString("cardimage")));
                        map.put("categoryid", (String) c.getString("categoryid"));
                        MyArrList.add(map);
                    }

                    int currentPosition = gridView1.getFirstVisiblePosition();
                    ImageAdapter adapter = new ImageAdapter(CardActivity.this, MyArrList);
                    gridView1.setAdapter(adapter);
                    gridView1.setSelection(currentPosition + 1);

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        return null;
    }

    protected void onPostExecute(Void unused) {
        ShowAllContent();
        dismissDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
        removeDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
    }

}
  • 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-17T02:03:57+00:00Added an answer on June 17, 2026 at 2:03 am

    You should probably move this code:

                    int currentPosition = gridView1.getFirstVisiblePosition();
                    ImageAdapter adapter = new ImageAdapter(CardActivity.this, MyArrList);
                    gridView1.setAdapter(adapter);
                    gridView1.setSelection(currentPosition + 1);
    

    into the onPostExecute method, because it has to be run on UI thread

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

Sidebar

Related Questions

i'm developing an app for android and want using GridView build a menu with
I'm using a GridView in my Android app. Everything works perfectly on my phone,
I'm running an app that pulls images using a URL into a gridView, the
I have an ASP.NET(2.0 using C#) web app, in which I have a gridview.
I am using the standard gridview source for my app. The problem which i
I am able to open my image from SD card in gridview and also
While I was developing a startscreen for my app using the GridView control, I
I am developing a WinRT app in c# , I am using a GridView
I am using json to run a function in code behind of my app.
I'm using the following code to grab images from the web. It uses Gridview

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.