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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:29:46+00:00 2026-05-24T00:29:46+00:00

So my code seems to run just fine until it hits this line adapter.notifyDataSetChanged();

  • 0

So my code seems to run just fine until it hits this line

adapter.notifyDataSetChanged();

The error that pops up in the logcat is CalledFromWrongThreadException. The debug also shows the handler being run in the Background thread. How do I get the handler to bind to the main thread, and not the background one? I thought I just had to create the handler in the main thread, but I guess I am wrong, quite possible I am new to andriod. How do I fix this?

//Imports are included
public class DirectoryActivity extends ListActivity {
    private ProgressDialog ProgressDialog = null;
    private ArrayList<DirectoryListing> listing = null;
    private DirectoryAdapter adapter;
    private Runnable viewOrders;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.directory);
    final Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {


            if (listing != null && listing.size() > 0) {
                adapter.notifyDataSetChanged();
                for (int i = 0; i < listing.size(); i++)
                    adapter.add(listing.get(i));
                Log.e("log_tag", "\nStill running\n");
            }
            ProgressDialog.dismiss();
            adapter.notifyDataSetChanged();

        }

    };
    listing = new ArrayList<DirectoryListing>();
    adapter = new DirectoryAdapter(this, R.layout.rows, listing);
    setListAdapter(adapter);
    ProgressDialog = ProgressDialog.show(DirectoryActivity.this, "Please wait...", "Retrieving data ...", true);
    viewOrders = new Runnable() {
        @Override
        public void run() {
            listing = PreparePage.getArrayList();
            handler.handleMessage(null);
        }
    };
    Thread thread = new Thread(null, viewOrders, "Background");
    thread.start();




}


private static class PreparePage {
    protected static ArrayList<DirectoryListing> getArrayList() {

        ArrayList<DirectoryListing> listings = new ArrayList<DirectoryListing>();
        JSONObject information = GetPageData.getJSONFromURL(url);
        Iterator key = information.keys();

        while (key.hasNext()) {
            String id = (String) key.next();
            JSONObject info = null;
            try {
                info = information.getJSONObject(id);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String name = "", title = "", photo = "";
            try {
                name = info.get("firstName") + " " + info.get("lastName");
                title = info.getJSONObject("position").getString("name");
                photo = info.optString("photoPath", "none");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            listings.add(new DirectoryListing(name, title, photo));

        }
        return listings;
    }
}

}

  • 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-24T00:29:47+00:00Added an answer on May 24, 2026 at 12:29 am

    Try calling handler.sendEmptyMessage(0); instead of handler.handleMessage(null);

    I don’t know why this would cause the errors you are seeing, but this is how I have it set up when I use handler and thread instead of AsyncTask. And I have have never seen that error doing it this way.

    @Nguyen is right though AsyncTask is the preferred way to handle these types of things now. And it actually makes it much easier to do.

    AsyncTask docs

    AsyncTask Example

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

Sidebar

Related Questions

I'm seeing an error with Core Data that seems to indicate that the old
I am creating an Activiti controller that can listen for messages and fork off
I'm trying to implement a piece of code to synchronously start looped service in
In my learning I have always learnt and understood callbacks in jQuery like this:
I'm trying to write a Windows service that runs indefinitely. Windows forms and background
I have recently completed development on a game for Android smartphones. It was designed
When the user launches my Android App, I fire up 2 threads to do
TLDR : Can the onreadystatechange event be emulated somehow else for synchronous requests without
I'm a Java developer by trade and my exposure to C++ / JNI has

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.