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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:27:33+00:00 2026-06-11T00:27:33+00:00

This is my code: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, onCreate); setContentView(R.layout.list);

  • 0

This is my code:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate");
        setContentView(R.layout.list);    
        new GetBlockListAsyncTask().execute(BlockListActivity.this);
    }
public void initializeDialog() {    
        dialog = ProgressDialog.show(BlockListActivity.this, "", "Loading data. Wait...", true);
        dialog.show();
    }

    public void dismissDialog(){        
        dialog.dismiss();
    }

The GetBlockListAsyncTask:

 public class GetBlockListAsyncTask extends AsyncTask<Object, Boolean, String>{

private BlockListActivity callerActivity;
private String TAG = "GetBlockListAsyncTask";
private String stringCode = "";

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

    callerActivity = (BlockListActivity)params[0];      
    try {
        Log.d(TAG, "Start to sleep");
        Thread.sleep(4000);         
        Log.d(TAG, "End sleep");
    } catch (InterruptedException e) {
       e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(String response) {
    callerActivity.dismissDialog();
}

@Override
protected void onPreExecute() {
    callerActivity.initializeDialog();
}

}

It will show error:

'Caused by: java.lang.NullPointerException'
  onPreExecute(GetBlockListAsyncTask.java:101)

I find a solution is that if I move the initializeDialog out of the AsyncTask and put it before the line new GetBlockListAsyncTask().execute(BlockListActivity.this); in onCreate, it works.
The question is how to make it work if I want to put the initializeDialog in the AsyncTask .

  • 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-11T00:27:34+00:00Added an answer on June 11, 2026 at 12:27 am

    Try adding a public constructor to your AsyncTask that accepts the Activity Context as the first argument:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Create a new AsyncTask with the Activity Context
        AsyncTask task = new GetBlockListAsyncTask(this);
    
        // Execute the task
        task.execute();
    }
    
    public class GetBlockListAsyncTask extends AsyncTask<Object, Boolean, String> {
        private Context activityContext;
    
        private String TAG = "GetBlockListAsyncTask";
        private String stringCode = "";
    
        //Constructor
        public GetBlockListAsyncTask(Context c) {
            // Store the activity context
            activityContext = c;
        }
    
        @Override
        protected String doInBackground(Object... params) { 
            try {
                Log.d(TAG, "Start to sleep");
                Thread.sleep(4000);         
                Log.d(TAG, "End sleep");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        @Override
        protected void onPostExecute(String response) {
            activityContext.dismissDialog();
        }
    
        @Override
        protected void onPreExecute() {
            activityContext.initializeDialog();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my code: Activity: @Override public void onCreate(Bundle savedInstanceState) { ... Intent intent
I have this code: @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { GLES20.glClearColor(0, 0,
In facebook tutorials I see this sample code: @Override public void onActivityResult(int requestCode, int
Is it possible to change this code: @Override public boolean onKeyDown(int keyCode, KeyEvent event)
i found this code: protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource hwndSource =
I have onPause in activity and works great. This is code @Override protected void
I have this code running in my onDestroy function: @Override protected void onDestroy() {
Log.d(TAG, Build.VERSION_CODES.ICE_CREAM_SANDWICH: + Build.VERSION_CODES.ICE_CREAM_SANDWICH); I write code like this, I used the sdk4.0 to
In this code (from the WCF REST starterkit - preview2): protected override SampleItem OnAddItem(SampleItem
This is my code. I want to override the Draw method import wx.grid as

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.