0

我有一个列表活动。当我单击此列表活动的一个项目时,调用的活动需要时间来加载,因此我想添加一个加载对话框,该对话框应在活动开始后立即消失。但我做不到。我也很困惑在列表活动或被调用活动中添加对话框。在将对话框添加到后面的活动时,我收到此错误:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@40521cd0 is not valid; is your activity running?

这就是我插入对话框的方式

    super.onCreate(savedInstanceState);         
        setContentView(R.layout.graphview);     
        ProgressDialog pdialog=new ProgressDialog(this);
         pdialog.setCancelable(true);
         pdialog.setMessage("Loading ....");
         pdialog.show();

请告诉我出了什么问题。

这是我的完整代码:

 public class List12 extends ListActivity {

     private List<String> item = null;
     private List<String> path = null;
     private String root;
     private TextView myPath;
     private ProgressDialog mProgressDialog;
     public static final int DIALOG_DOWNLOAD_PROGRESS = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle b=this.getIntent().getExtras(); 
        final String path=(String) b.get("key"); 
        setContentView(R.layout.list13);
        myPath = (TextView)findViewById(R.id.path);


            root = Environment.getExternalStorageDirectory().getPath()+"/"+path+"/raw/";        

            File f = new File(root);
            File[] files = f.listFiles();
            if(files==null)
            {
                Toast.makeText(this, "No stored records for the patient", Toast.LENGTH_LONG).show();
                List12.this.finish();
            }
            // TODO Auto-generated catch block
            else
            getDir(root, path);

    }

    private void getDir(String dirPath, String id)
    {
     myPath.setText("Stored data for "+id);
     item = new ArrayList<String>();
     path = new ArrayList<String>();
     File f = new File(dirPath);
     File[] files = f.listFiles();

     if(!dirPath.equals(root))
     {
      item.add(root);
      path.add(root);
      item.add("../");
      path.add(f.getParent()); 
     }

     for(int i=0; i < files.length; i++)
     {
      File file = files[i];

      if(!file.isHidden() && file.canRead()){
       path.add(file.getPath());
          if(file.isDirectory()){
           item.add(file.getName() + "/");
          }else{
           item.add(file.getName());
          }
      } 
     }

     ArrayAdapter<String> fileList =
       new ArrayAdapter<String>(this, R.layout.row, item);
     setListAdapter(fileList); 
    }@Override
     protected void onListItemClick(ListView l, View v, int position, long id) {
      // TODO Auto-generated method stub
      File file = new File(path.get(position)); 
      Bundle b=new Bundle();    
      String array = file.getAbsolutePath();
      b.putString("key",array); 
      b.putBoolean("flag", false);
      Intent in = new Intent(getParent(), Display.class);
      TabGroupActivity parentActivity = (TabGroupActivity)getParent();
      in.putExtras(b);  
      parentActivity.startChildActivity("Dsiplay", in);
      ProgressDialog pdialog=new ProgressDialog(List12.this);
      pdialog.setCancelable(true);  
    pdialog.setMessage("Loading ...."); 
      pdialog.show();
     }}
4

2 回答 2

2

尝试使用 AsyncTask 作为子类

class yourFileOperationTask extends AsyncTask<String, String, String> {

 @Override
 protected void onPreExecute(String result) {
 //create and show your progress dialog here
    }
 @Override
 protected String doInBackground(String... params) {  
 // Do the stuff here(your file operation)
  }  
 @Override
 protected void onPostExecute(String result) {
 //dismiss the dialog and  start the intent.
    }
}

然后在您的 onclick 中执行此操作:

//note that you can change the parameter types of your AsyncTask
new yourFileOperationTask().execucte(stringArrayParameter);
于 2012-11-06T09:41:35.403 回答
0

将您的进度对话框放在点击侦听器中例如:

public class MyList extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    Button btn  = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ProgressDialog pdialog=new ProgressDialog(MyList.this);
             pdialog.setCancelable(true);
             pdialog.setMessage("Loading ....");
             pdialog.show();
        //Rest of the code
        }
    });
}

}
于 2012-11-06T07:58:24.033 回答