简体   繁体   English

当我删除进度对话框时,它将起作用。 为什么?

[英]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. 没有您的LogCat日志,我只能猜测。

m_ProgressDialog is defined after you start your thread. 在启动线程之后定义m_ProgressDialog Why? 为什么? Define it before the thread is started. 在线程启动之前定义它。

Also, I would recommend an AsyncTask for this, instead. 另外,我会为此推荐一个AsyncTask See Painless Threading for details on that. 有关详细信息,请参见无痛线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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