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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:25:54+00:00 2026-05-26T16:25:54+00:00

I have the following code but I get an error on this line userSpinner.setAdapter(adapter);

  • 0

I have the following code but I get an error on this line “userSpinner.setAdapter(adapter);”

 private class Task extends AsyncTask<Void, Void, Void> 
 { 
     protected void onPreExecute() {            
     showDialog(DIALOG_TASKING); 
     } 
     protected Void doInBackground(Void... JSONArray) { 

        try
        {
        HttpGet request = new HttpGet(SERVICE_URI + "/GetBusNames");
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);
        HttpEntity responseEntity = response.getEntity();

        char[] buffer = new char[(int)responseEntity.getContentLength()];
        InputStream stream = responseEntity.getContent();       
        InputStreamReader reader = new InputStreamReader(stream);
        reader.read(buffer);
        stream.close();

        JSONObject jsonResponse = new JSONObject(new String(buffer));
        JSONArray myUsers = jsonResponse.getJSONArray("GetBusNamesResult");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(RealestateActivity.this, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        adapter.add("Select a Buseness...");                
        for (int i = 0; i < myUsers.length(); ++i)
        {
            adapter.add(myUsers.getString(i));
            adapter.add(myUsers.getJSONObject(i).getString("BusName"));
        }

            userSpinner.setAdapter(adapter); // I get an error here if I wrap these two lines in /*...*/ the whole thing loads as expected but the spinner is empty
            userSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

        }
        catch (Exception e) 
        {       
            e.printStackTrace();
            displayExceptionMessage(e.getMessage());
        }
        return null;
    }

    protected void onPostExecute(Void unused) {
        dismissDialog(DIALOG_TASKING);              
    } 

}

The following is the Stack Trace produced,

11-07 19:54:35.300: ERROR/AndroidRuntime(8741): FATAL EXCEPTION: AsyncTask #1
11-07 19:54:35.300: ERROR/AndroidRuntime(8741): java.lang.RuntimeException: An error occured while executing doInBackground()
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at java.lang.Thread.run(Thread.java:1096)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at android.os.Handler.<init>(Handler.java:121)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at android.widget.Toast.<init>(Toast.java:68)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at android.widget.Toast.makeText(Toast.java:231)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at com.fnesse.realestate.RealestateActivity.displayExceptionMessage(RealestateActivity.java:271)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at com.fnesse.realestate.RealestateActivity$Task.doInBackground(RealestateActivity.java:131)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at com.fnesse.realestate.RealestateActivity$Task.doInBackground(RealestateActivity.java:1)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-07 19:54:35.300: ERROR/AndroidRuntime(8741):     ... 4 more
11-07 19:56:22.714: ERROR/PowerManagerService(2464): CurLock p:3 mPS:1
11-07 20:06:26.577: ERROR/libnetutils(2464): dhcp start cmd 11 : [dhcpcd:-ABK] 
11-07 20:06:27.054: ERROR/HierarchicalStateMachine(2464): TetherMaster - unhandledMessage: msg.what=3

The code works in its own method but not in the AsyncTask.

Any idea’s.

Cheers,

Mike.

  • 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-26T16:25:55+00:00Added an answer on May 26, 2026 at 4:25 pm

    You can’t perform Display/Update UI inside the doInBackground() method, instead either you can perform by implement runOnUIThread() method or write display/Update statement inside the onPostExecute() method.

    In your case, write below statement inside the onPostExecute() and declare JSONArray myUsers at class level:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(RealestateActivity.this, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            adapter.add("Select a Buseness...");                
            for (int i = 0; i < myUsers.length(); ++i)
            {
                adapter.add(myUsers.getString(i));
                adapter.add(myUsers.getJSONObject(i).getString("BusName"));
            }
    
                userSpinner.setAdapter(adapter); // I get an error here if I wrap these two lines in /*...*/ the whole thing loads as expected but the spinner is empty
                userSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    
            }
            catch (Exception e) 
            {       
                e.printStackTrace();
                displayExceptionMessage(e.getMessage());
            }
    

    Final Solution:

    private class Task extends AsyncTask<Void, Void, Void> 
     { 
        JSONArray myUsers = null;
        JSONObject jsonResponse = null;  // this is also needed at class level
    
         protected void onPreExecute() {            
             showDialog(DIALOG_TASKING); 
         } 
         protected Void doInBackground(Void... JSONArray) { 
    
            try
            {
            HttpGet request = new HttpGet(SERVICE_URI + "/GetBusNames");
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");
    
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);
            HttpEntity responseEntity = response.getEntity();
    
            char[] buffer = new char[(int)responseEntity.getContentLength()];
            InputStream stream = responseEntity.getContent();       
            InputStreamReader reader = new InputStreamReader(stream);
            reader.read(buffer);
            stream.close();
    
            jsonResponse = new JSONObject(new String(buffer));
            }
            catch (Exception e) 
            {       
                e.printStackTrace();
                displayExceptionMessage(e.getMessage());
            }
            return null;
        }
    
        protected void onPostExecute(Void unused) {
            dismissDialog(DIALOG_TASKING);    
    
     myUsers = jsonResponse.getJSONArray("GetBusNamesResult");
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(RealestateActivity.this, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            adapter.add("Select a Buseness...");                
            for (int i = 0; i < myUsers.length(); ++i)
            {
                adapter.add(myUsers.getString(i));
                adapter.add(myUsers.getJSONObject(i).getString("BusName"));
            }
    
                userSpinner.setAdapter(adapter); // I get an error here if I wrap these two lines in /*...*/ the whole thing loads as expected but the spinner is empty
                userSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());          
        } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, but I get an error on on the last
i have the following code to disable task manager of windows xp but it
I have the following code to do this, but how can I do it
I have the following code I want to run, but the problem is $this->type
In the following code i have error Uncaught SyntaxError: Unexpected token ; in line
I have the following code snippet.. I get the error Expected identifier, string or
I have the following code: $this->Permissions->updateAll( array('Permissions.user' => $newuser), array('Permissions.user' => $originaluser) ); But
In a php page I have following code: if($_REQUEST['c']!=) // I get error on
I have the following code but the lon/lat seems to be returning null; package
I have tried the following code but has no effect: Imports system.Runtime.InteropServices <DllImport(UxTheme.DLL, BestFitMapping:=False,

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.