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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:49:34+00:00 2026-06-12T10:49:34+00:00

In my app i have an activity(MainActivity.java) and a class(non-activity class – SubClass.java). The

  • 0

In my app i have an activity(MainActivity.java) and a class(non-activity class – SubClass.java). The SubClass has a ProgressDialog. i will call the SubClass from the MainActivity.

My question is How do i use the ProgressDialog from the Non-Activity Class(SubClass.java) from the Activity Class (MainActivity.java)

I get Null Pointer Exception Error at setProgress() and package declaration.

I did the following things

1) In the MainActivity i create object for the SubClass with constructor.

    SubClass pro = new SubClass(MainActivity.this);
    pro.call_fun();

2) In the SubClass : the whole coding ;

package com.progresss;  // Line No : 1 ; Shows Null Pointer Exception
public class SubClass {
Context context = null;
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private NotificationManager notificationManager;
private Notification notification;
private int progress_val = 0;
private String fileURL = "http://xxxxx.com/folder/sampleproject.apk";
private String destination = null;
private ProgressDialog downloadProgressDialog;

public SubClass(MainActivity progress_DialiogActivity) {
    this.context = progress_DialiogActivity;
}

public void call_fun(){
    new DownloadFileAsync().execute(fileURL);
}

    //  Updated APK File Download Task.
class DownloadFileAsync extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        ((Activity) context).showDialog(DIALOG_DOWNLOAD_PROGRESS);
    //          Notification Coding     
        ...........
        notification.contentIntent = contentIntent;
    //          -------------------            
        notificationManager =(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notification);
    }

    @Override
    protected String doInBackground(String... aurl) {
        try {
    //              .......         
        } catch (Exception e) {
            Log.e("Error Report Download Manager", e.getMessage());
          }
        return null;
    }

    protected void onProgressUpdate(String... progress) {
         progress_val = Integer.parseInt(progress[0]);
         .......
         downloadProgressDialog.setProgress(Integer.parseInt(progress[0]));   // Line No : 100 ; Shows Null Pointer Exception
    }

    @Override
    protected void onPostExecute(String unused) {
        notificationManager.cancel(0);
    //          ((Activity) context).dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
        ((Activity) context).removeDialog(DIALOG_DOWNLOAD_PROGRESS);
        Log.e("Download", "Stop");

    //          Notification Coding     
        notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        .........
        notificationManager.notify(0, notification);

    //          Open The Install App Activity(Intent) Here.            
        ........
    }
} // Async Task Class End

    //our progress bar settings
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS: //we set this to 0
            downloadProgressDialog = new ProgressDialog(context);
            downloadProgressDialog.setMessage("Downloading file...");
            downloadProgressDialog.setMax(100);
            downloadProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            downloadProgressDialog.setCancelable(false);
            downloadProgressDialog.show();
            return downloadProgressDialog;
        default:
            return null;
    }
  }
}

This is LogCat Error Message:

    FATAL EXCEPTION: main
java.lang.NullPointerException
at com.progresss.SubClass$DownloadFileAsync.onProgressUpdate(SubClass.java:100)
at com.progresss.SubClass$DownloadFileAsync.onProgressUpdate(SubClass.java:1)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

The Progress Dialog Code Works good in an activity class. i guess ProgressDialog may not be initialized correctly. Please help me.

  • 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-12T10:49:36+00:00Added an answer on June 12, 2026 at 10:49 am

    onCreateDialog is not executed. Please move the code to onPreExecute Method .

    onCreateDialog is call back method and it will be called in activity only , in your subcalss its just a method.

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

Sidebar

Related Questions

I have an app with : an activity class that allows the user to
I am making an app in which i have a main activity that the
I am making an android app. I have a main activity and i want
I have an android app where in my Main activity I can play music
in my app i have activity with XML layout, i want to build an
In my app have a sign in activity. If login is successful, I start
I have an app that uses the Graph API to post activity to a
I have an app that contains several activities...so lets say user is navigating activity
I have an app that displays a splash screen. The splash screen activity creates
I have an app that, after some clicking, shows activity with news contents. I

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.