0

My simple app is set to retrieve RSS feeds when the Refresh button is clicked.
Clicking the refresh button starts an AsyncTask which retrieves the feeds. The AsyncTask is a subcalss of the main activity.

Here is what my class looks like:

public class NasaDailyImage extends Activity{
    public ProgressDialog modalDialog = null; //problematic
//------------------------------------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState){
        //instantiate the ProgressDialog
        Button b = //get reference to button
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                modalDialog.show(); // show modal
                Toast.makeText(getApplicationContext(), "Getting feeds", 500).show();
                new AsyncRetriever().execute(new IotdHandler()); // Get the feeds !!
            }
        });
    }
//------------------------------------------------------------------------------
    public synchronized void resetDisplay(boolean parseErrorOccured, 
        boolean imageErrorOccured,
        IotdHandler newFeeds){
        if(parseErrorOccured || imageErrorOccured){
            // make a Toast
            // do not update display
        }else{
            // make a Toast
            // update display
            // based on new feed
        }
    }
//------------------------------------------------------------------------------
    class AsyncRetriever extends AsyncTask<IotdHandler,Void,IotdHandler>{

        @Override
        protected IotdHandler doInBackground(IotdHandler... arg0) {
            IotdHandler handler = arg0[0];
            handler.processFeed(); // get the RSS feed data !
            return handler;
        }
//------------------------------------------------------------------------------    
        @Override
        protected void onPostExecute(IotdHandler fromInBackground){
            resetDisplay( // call to update the display
            fromInBackground.errorOccured,
            fromInBackground.imageError,
            fromInBackground);
        }
//------------------------------------------------------------------------------


}  

The problem is with show() of modalDialog in the onCLickListener. That causes my app to crash. Why? and how do I solve it?

An SO User
  • 24,612
  • 35
  • 133
  • 221

3 Answers3

2

Your ProgressDialog is null so you get NPE when you call show() on it. Instantiate it before calling a method on it.

Probably somewhere like here instantiate it then set the message, title, whatever you want before showing it

 @Override
protected void onCreate(Bundle savedInstanceState){
    //instantiate the ProgressDialog

   // can set message, title, etc. here

    Button b = //get reference to button
   b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) { 
    modalDialog = ProgressDialog.show(NasaDailyImage .this, "Enter Title", "Enter Message");
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Ok a few other questions: Why doesnt app crash when I make a `Toast` in `onClickListener` ?? That isn't on the UI thread right ? AFAIK only UI thread can make changes to UI – An SO User Jul 24 '13 at 18:50
  • That is on the `UI Thread`. You also need to call `show()` in a static way – codeMagic Jul 24 '13 at 18:51
  • onClick method is working inside UI thread. Your toast works, because you instantiated it using this: Toast.makeText(...). While your modalDialog is null – Geralt_Encore Jul 24 '13 at 18:55
  • Everything I see in your code so far is on the `UI Thread` except for `doInBackground()` – codeMagic Jul 24 '13 at 18:57
2

Cannot update ui on the background thread. doinBAckground is invoked on the background thread. You should use progress dialog to show like Async Task and then you can change anything in your user interface (UI).

Step 1 : First initialize your your progress dialog.

ProgressDialog progressDialog;

Step 2: Then start your doBackground work below the progress dialog start.

progressDialog = ProgressDialog.show(Activity1.this, "",
                        "Loading...");

Step 3: Finally dismiss your progress dialog after your works done.

progressDialog.dismiss();

In the mean time of progress dialog you can change any ui

Mahmudul
  • 470
  • 1
  • 4
  • 12
1

You need to instantiate ProgressDialog:

modalDialog = ProgressDialog.show(this, null, "Please, wait...");
Geralt_Encore
  • 3,721
  • 2
  • 31
  • 46