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

  • Home
  • SEARCH
  • 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 9218521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:53:01+00:00 2026-06-18T02:53:01+00:00

I made an application for Android that originally targeted a lower version (2.3). After

  • 0

I made an application for Android that originally targeted a lower version (2.3). After I got my proof-of-concept working, I tried to get it to work on Android 4. That’s when I got the NetworkOnMainThread exception.

After doing some research, I quickly found the AsyncTask, which sounded awesome. The problem is, I’m having a hard time wrapping my mind around it. For instance, here’s my original code:

public void Refresh(Context c)
{
    SummaryModel model = MobileController.FetchSummary(c);

    TextView txtCurrentWeight = (TextView)findViewById(R.id.txtCurrentWeight);
    TextView txtWeightChange = (TextView)findViewById(R.id.txtWeightChange);
    TextView txtAvgPerWeek = (TextView)findViewById(R.id.txtAvgPerWeek);

    if(model.ErrorMessage == "")
    {
        txtCurrentWeight.setText(model.CurrentWeight);
        txtWeightChange.setText(model.WeightChange);
        txtAvgPerWeek.setText(model.Average);
    }
    else
    {
        Toast.makeText(c, model.ErrorMessage, Toast.LENGTH_LONG).show();

        txtCurrentWeight.setText("");
        txtWeightChange.setText("");
        txtAvgPerWeek.setText("");
    }
}

I created an AsychTask like this:

public class WebMethodTask extends AsyncTask<Object, Integer, Object> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }

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

        SummaryModel model = (SummaryModel)result;
        // Can't seem to access UI items here??

    }

    @Override
    protected Object doInBackground(Object... params) {

        Context c = (Context)params[0];
        return MobileController.FetchSummary(c);
    }
}

How do I access the UI items from the onPostExecute method? Or, do I have the wrong idea on how to use AsyncTask?

Thanks!

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

    You should be able to accessUI where you put your comments (in the postExecute method)

    Additionally, I would suggest to use more specialized class with for AsyncTask, so that your code looks better :

    public class WebMethodTask extends AsyncTask<Object, Integer, SummaryModel> {
    
        private Activity source;
        public WebMethodTask(Activity activity) {
            this.source=activity;
            }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }
    
        @Override
        protected void onPostExecute(SummaryModel model) {
            super.onPostExecute(model );
    
            TextView txtCurrentWeight = (TextView)source.findViewById(R.id.txtCurrentWeight);
            TextView txtWeightChange = (TextView)source.findViewById(R.id.txtWeightChange);
            TextView txtAvgPerWeek = (TextView)source.findViewById(R.id.txtAvgPerWeek);
    
            if(model.ErrorMessage.length()==0)
            {
                txtCurrentWeight.setText(model.CurrentWeight);
                txtWeightChange.setText(model.WeightChange);
                txtAvgPerWeek.setText(model.Average);
            }
            else
            {
                Toast.makeText(c, model.ErrorMessage, Toast.LENGTH_LONG).show();
    
                txtCurrentWeight.setText("");
                txtWeightChange.setText("");
                txtAvgPerWeek.setText("");
            }
    
        }
    
        @Override
        protected SummaryModel doInBackground(Context ... params) {
            Context c = params[0];
            return MobileController.FetchSummary(c);
        }
    }
    

    Edit : Added a reference to your activity, to take your last comment into account.


    However, if you acynctask can be long, it’s maybe not a very good idea to keep a reference on an activity.

    It would be a better design to create a listenerclass that will accept some displayModel(CummaryModel) method, and whose responsability is to cal the setText methods if the activity has not been paused / stopped in the meanwhile…

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

Sidebar

Related Questions

I have made an application in android that lets the user compress and decompress
I made an android application inside that i have put an apk file in
I am working on Android application that has several threads - one of them
I'm working on an Android application that will send MMS internally without using the
I made ​​an Android application that uses that uses the SearchManager. The problem is
Last year I made an Android application that scrapped the informations on my train
I made a simple android application that moves from one activity to another activity
I am working on a very simple application in Android. I made a splash
I have made an Android application that sends data over networks using sockets. I'm
I've made an Android application that uses a X509 certificate (that is in the

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.