2012-01-31 67 views
0

我使用該組在一個選項卡中顯示活動。 A和B活動是同一組。 呼叫乙如下面的代碼:加載時顯示對話框

Intent intent = new Intent(AActivity.this, BActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
Window w = MyGroup.group.getLocalActivityManager().startActivity("BActivity", intent); 
View view = w.getDecorView(); 
MyGroup.group.setContentView(view); 

而且BActivity是ListActivity。

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
this.setContentView(R.layout.list); 
RL url = new URL(urlstr); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
connection.setConnectTimeout(10000); 
connection.setDoInput(true); 
connection.setDoOutput(true); 
connection.setUseCaches(false); 
InputStream is = connection.getInputStream(); 
//extract information from is, and show in list view 
} 

我想顯示對話框顯示加載。 我已經嘗試了AsyncTask和Thread Runnable方法。 但是錯誤Unable to add window -- token [email protected] is not valid; is your activity running?顯示。 如何解決它?

回答

1

您可以在Handler Class的幫助下使用ProgressDialog類。這樣你就可以實現你想要做的事情。

progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true); 

new Thread (new Runnable() 
{ 
    public void run() 
    { 
     // your loading code goes here 
    } 
}).start(); 


    Handler progressHandler = new Handler() 
    { 

     public void handleMessage(Message msg1) 
     { 

      progDailog.dismiss(); 
      } 
    } 
+0

它具有相同的錯誤。 – brian 2012-01-31 08:58:06

+0

看到我已添加代碼 – Lucifer 2012-01-31 09:04:04

2

我假設你在一個TabHost中。因此,添加ProgressDialog時,請勿使用Activities Context this添加它,而應使用getParent()來獲取TabHost的上下文:
ProgressDialog pDia = new ProgressDialog(getParent());

+0

但我仍然沒有得到答案。 – brian 2012-01-31 08:57:25

+0

使用AsynTask來執行您的下載代碼。在onPreExecute中,您必須在上面添加ProgressDialog代碼。 – Thommy 2012-01-31 09:19:09