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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:24:35+00:00 2026-06-12T15:24:35+00:00

Consider this DialogFragment: public class RollTriggerDialog extends DialogFragment{ private ProgressDialog _dialog; int _progress; public

  • 0

Consider this DialogFragment:

public class RollTriggerDialog extends DialogFragment{

    private ProgressDialog _dialog;
    int _progress;
    public Handler _progressHandler;

    public RollTriggerDialog() {
        // empty
    }

    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        _dialog = new ProgressDialog(getActivity());

        this.setStyle(STYLE_NO_TITLE, getTheme());
        _dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        _dialog.setProgress(0);

        _progressHandler = new Handler() {
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (_progress >= 100) {
                    _dialog.dismiss();
                } else {
                    _progress++;
                    _dialog.incrementProgressBy(1);
                    _progressHandler.sendEmptyMessageDelayed(0,100);
                }

            }
        };
        //_progressHandler.sendEmptyMessage(0); <- This uncommented would start the progress
        return _dialog;
    }
}

It is just a horizontal progressbar with a handler, once the handler receives one message the progressbar goes from 0 to 100.

I am always getting a Null Pointer Exception if I want to trigger that sendEmptyMessage by myself from an activity:

public class MainActivity extends FragmentActivity {

    private RollTriggerDialog mRollTriggerDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getSupportFragmentManager();
        mRollTriggerDialog = new RollTriggerDialog();
        mRollTriggerDialog.show(fm, "addDiceDialog_tag");
        ((RollTriggerDialog)fm.findFragmentByTag("addDiceDialog_tag"))._progressHandler.sendEmptyMessage(0); // <--- NPE HERE!
    }
}

If the line of sendEmptyMessage is uncommented in the dialogFragment and the line with NPE in the main activity is commented; the app runs. What is wrong with that invocation?

Note that this is the whole code, excepting manifest and layout files.

  • 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-12T15:24:36+00:00Added an answer on June 12, 2026 at 3:24 pm

    The NullPointerException appears because the findFragmentByTag returns null. The solution is to call fm.executePendingTransactions() before you use the findFragmentByTag method to execute that fragment transaction right away(see this question for more details).

    Also, the Handler reference will be null at that moment so you’ll want to initialize it in one of the fragment’s lifecycle methods, for example, onCreate:

    public static Handler _progressHandler; // should be made static
    
    //...
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
        _progressHandler = new Handler() {
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
    
                if (_progress >= 100) {
                    _dialog.dismiss();
                } else {
                    _progress++;
                    _dialog.incrementProgressBy(1);
                    _progressHandler.sendEmptyMessageDelayed(0, 100);
                }
    
            }
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this simple example - public class Person { private String name; private Date
Consider this code snippet public class ConstantFolding { static final int number1 = 5;
Consider this: class Vec3 { private: float n[3]; public: float& x; float& y; float&
Consider this class: public class Foo { // Fields private string _bar; // Properties
Consider this: public class TestClass { private String a; private String b; public TestClass()
Consider this syntactically correct(?) pseudocode: class Event { public: virtual int getID() const =
Consider this: public class Minesweeper extends MIDlet implements CommandListener { public static String error
consider this sample code: 1. public class GC { 2. private Object o; 3.
Consider this code: public static class PredefinedStrings { public const string Golf = Golfing;
Consider this simple class: package net.zomis.test; public class Test { public Test() { }

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.