简体   繁体   中英

Android Progress dialog before listview

i am trying to do progress dialog before listview are showed because items on listview are downloaded.

I get this because i'm trying to change listview in thread with displayListView();

02-08 00:25:48.959: W/System.err(3244): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

If i put displayListView() outside my thread i get nullpointerexeption. displayListView() uses fieldsList arraylist which are downloaded. I've tried to wait while myThread.isAlive, but in this way progressdialog isn't showed. How to fix it?

My code:

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.build_main);
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setCancelable(true);
        dialog.setMessage("Loading...");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setProgress(0);
        dialog.setMax(100);
        dialog.show();


        Thread myThread = new Thread(new Runnable() {
            public void run() {


                Builder builder = new Builder(server, user, password, BuildFieldsViewActivity.this, USER_AGENT);
                Document doc = null;
                doc = builder.DownloadPage("d.php?ne="+gyv_id);

                fieldsList = builder.getKaimas(doc);

                displayListView();

                dialog.setProgress(100);
                dialog.dismiss();
            }
        });
        myThread.start();
}

any UI element must be updated from the UI thread. You are manipulating the dialog from a background thread. Thus, crash happened.

a quick fix would be to use runOnUiThread(Runnable), inside the runnable, you can update any UI element safely.

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