开发者

progressdialog preventing contentview switch?

Whenever I attempt to set content view after dismissing the progress dialog I get an error like...

04-13 14:40:38.043: WARN/System.err(801): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 04-13 14:40:38.073: WARN/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44dde3f0

My code is like...

ProgressDialog dialog = null; //class variable.


dialog = ProgressDialog.show(Login.this, "", 
            "Logging you in. Please wait...", true);
     new Thread() {
         public void run() {
                 try{
                      //serious work here!
                    }
                    dialog.dismiss();
                开发者_JS百科    setContentView(R.layout.blaa);
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
         }
 }.start();

What's causing this?


Your Thread are not the UI-thread. I think you need to create a Handler and then post a Runnable to it when you need to switch content view.

Handler hdl = new Handler(); // Will ron on UI-thread

dialog = ProgressDialog.show(Login.this, "", 
        "Logging you in. Please wait...", true);
new Thread() {
     public void run() {
             try{
                  //serious work here!
                }
                hdl.post(new Runnable() {
                    public void run() {
                        dialog.dismiss();
                        setContentView(R.layout.blaa);
                    }
                });
             } catch (Exception e) {
                 e.printStackTrace();
             }
     }
}.start();


You can use it like so:

 //Initialise
        private ProgressDialog m_ProgressDialog = null; 
        private Runnable myRunnable;

    //Make a runnable
        myRunnable= new Runnable(){
                    @Override
                    public void run(){
                        Thread.sleep(5000);
                        setContentView(R.layout.blaa);
                        m_ProgressDialog.dismiss();
                    }
                };


    //initialize your thread  
        Thread thread =  new Thread(null, myRunnable, "MagentoBackground");
        thread.start();

    //open up you dialog
        m_ProgressDialog = ProgressDialog.show(SelectStation.this,    
                        "", "Loading, please wait...", true);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜