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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:51:50+00:00 2026-06-16T13:51:50+00:00

I am using a subclass inside my main activity to do an asynctask. I

  • 0

I am using a subclass inside my main activity to do an asynctask. I plan to move it into a separate file afterwards I just prefer doing it this way to confirm it is working. I am trying to do a HTTPRequest with a progress dialog because of the potentially long waiting times. I am doing nothing in the doInBackground() because I’ve read you cannot access the UI layer from there. Ultimately what is happening is I show my progress dialog on onPreExecute() and dismiss it in onPostExecute(). I am calling each task separately above which could be 99% of my problem but I need to be able to pass my activity to the tasks for them to function properly.

I’ve broken a lot of the steps down into little pieces. I am getting a null pointer exception which leads me to believe my problem comes from not using execute() but I just can’t seem to figure out how that works.

new CodeRetrievalItem().onPreExecute(MyActivity.this);

new CodeRetrievalItem().onPostExecute(MyActivity.this);

class CodeRetrievalItem extends AsyncTask<Void, Void, Void>{

ProgressDialog dialog;
protected void onPreExecute(Activity actpass){
        dialog = new  ProgressDialog(actpass);
    dialog.setMessage("Loading");
    dialog.setIndeterminate(false); 
    dialog.setCancelable(false);
    dialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
    return null;
}

protected void onPostExecute(Activity actpass){

            // Execute HTTP Request
    try{
        dialog.dismiss();
    }
    catch(Exception e){
        Toast.makeText(actpass, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
    }

}

}
  • 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-16T13:51:52+00:00Added an answer on June 16, 2026 at 1:51 pm

    You are using AsyncTask completely wrong. There are several major mistakes in your code.

    AsyncTask protected methods

    Do not call them yourself. In the documentation for AsyncTask, you can find:

    Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params…), onProgressUpdate(Progress…) manually.

    You have to start your AsyncTask by calling execute(Params...) method. This will cause that:

    1. onPreExecute() is called prior to the start of background execution and is called on the main thread
    2. doInBackground(Params...) is called in background
    3. After background execution is finished onPostExecute(Result) is called on the main thread.

    Unless you call execute(Params...) nothing of this will happen.

    Generic parameters

    Moreover there is a problem with your generic parameters. AsyncTask has three generic paramters AsyncTask<Params, Progress, Result> (in your case AsyncTask<Void, Void, Void>, you have to match these parameters in the methods’ parameters.

    Therefore create:

    1. onPreExecute() instead of onPreExecute(Activity actpass)
    2. onPostExecute(Void) instead of onPostExecute(Activity actpass)

    Those methods you defined aren’t bound to the async execution in any way, they are just some extra methods in the class.

    If you need to access the activity inside the AsyncTask pass it in different way (e.g. in a constructor). Beware that passing an Activity to an AsyncTask might lead to possible memory leaks (it’s good to utilize WeakReference<T> here) and accessing dialogs in onPostExecute(Result) might cause crash if not properly handled.

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

Sidebar

Related Questions

I'm using an AsyncTask subclass for some background processing. The problem is that when
I am thinking about just using an imageview or subclass an UIView and override
I'm just started doing 'hands-on' learning of Actionscript 3 using FlashDevelop. I've now managed
I've just run into an issue when attempting to grab a custom View using
How can I draw two different rectangles with black background without using subclass of
I am using a TTPhotoViewController subclass from the Three20 library for showing images from
In an iPad app, I'm using a custom subclass of UIView with UIViewController. Here's
When using drawRect for a custom UIButton subclass, it never seems to get called
I'm using custom class that subclass from UILabel. It works fine unless I change
I'm using drawRect: in my UITableViewCell subclass for the rendering performance when scrolling; everything

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.