加载时显示对话框

时间:2012-01-31 08:10:41

标签: android

我使用该组在一个标签中显示活动。 A和B Activty是同一组。 一个叫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 android.app.LocalActivityManager$LocalActivityRecord@2afe9488 is not valid; is your activity running?显示。 怎么解决?

2 个答案:

答案 0 :(得分:2)

我假设你在TabHost中。因此,在添加ProgressDialog时,请不要使用活动上下文this来添加它,而是使用getParent()来获取TabHost的上下文:
ProgressDialog pDia = new ProgressDialog(getParent());

答案 1 :(得分: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();
             }
     }