简体   繁体   中英

When I remove progress dialog, it works. Why?

I am having a progress dialog for a process. But i am taking a null pointer exception in my thread. But, when i remove the progress dialog. I am no longer taking an exception.

My code is as this

public class PlayedActivity extends ListActivity {

private PullToRefreshListView listView;

final Context context = this;

public Handler handler;

Runnable sendNumbers2;

List<On> playedOn;

DatabaseHandlerOn db;

private ProgressDialog m_ProgressDialog;

private ArrayList<On> m_results = null;
private PlayedOnAdapter m_adapter;



    @SuppressLint({ "HandlerLeak", "HandlerLeak" })
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playedonnumara);


            db = new DatabaseHandlerOnNumara(getApplicationContext());

            m_results = new ArrayList<OnNumara>();

        this.m_adapter = new PlayedOnNumaraAdapter(this, R.layout.playedrowon, m_results);
        this.setListAdapter(this.m_adapter);

        sendNumbers2 = new Runnable() {

            @Override
            public void run() {

                playedOn = db.getAllContacts();      

                for (On on : playedOn) {

                    m_results.add(on);  

                }  


                Collections.reverse(m_results);

                //m_ProgressDialog.dismiss();

                handler.sendEmptyMessage(0);
            }
        };
            Thread thread = new Thread(sendNumbers2,"sendNumbers2");
            thread.start();

            /*m_ProgressDialog = ProgressDialog.show(PlayedOnNumaraActivity.this,
                    "",getString(R.string.PleaseWait), true);

            m_ProgressDialog.setCancelable(true);
        */

            handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {


                    m_adapter.notifyDataSetChanged();


                }

             };

        }
}

}

The code above is working and takes no exception when progress dialog codes are commented

Without your LogCat logs, I can only guess.

m_ProgressDialog is defined after you start your thread. Why? Define it before the thread is started.

Also, I would recommend an AsyncTask for this, instead. See Painless Threading for details on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM