未显示进度对话框

发布于 2024-11-11 00:37:00 字数 1696 浏览 4 评论 0原文

我有这段代码片段,这段代码的问题是当我单击“完成”按钮时 进度对话框未显示。请帮忙

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send);


        done = (Button)findViewById(R.id.sendbut);

        done.setOnClickListener(new OnClickListener() {             

            public void onClick(View v) {

                 showProgressDialog();
                 query_str = getString();
                         startUploading();
            }
        });          
 }

 public void showProgressDialog(){
     GlobalClass.printLine("Showing progress dialog"); 
     progressDialog = new ProgressDialog(Send.this);
     progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
     progressDialog.setMessage("Sending...");
     progressDialog.setCancelable(false);
     progressDialog.show();  
     // Start lengthy operation in a background thread
     new Thread(new Runnable() {
         public void run() {
                 // Update the progress bar
                 mHandler.post(new Runnable() {
                     public void run() {
                         progressDialog.setProgress(mProgressStatus);
                         Toast.makeText(getApplicationContext(), "Herererererer",Toast.LENGTH_LONG).show();
                         if(mProgressStatus == 100){                                             
                             Toast.makeText(getApplicationContext(), "Done",Toast.LENGTH_LONG).show();
                             progressDialog.dismiss();
                         }
                     }
                 });                                 
             }
     }).start(); 
 }

I have this code snippet, the problem with this code is that when I click the 'done' button
the ProgressDialog is not showing.Please help

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send);


        done = (Button)findViewById(R.id.sendbut);

        done.setOnClickListener(new OnClickListener() {             

            public void onClick(View v) {

                 showProgressDialog();
                 query_str = getString();
                         startUploading();
            }
        });          
 }

 public void showProgressDialog(){
     GlobalClass.printLine("Showing progress dialog"); 
     progressDialog = new ProgressDialog(Send.this);
     progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
     progressDialog.setMessage("Sending...");
     progressDialog.setCancelable(false);
     progressDialog.show();  
     // Start lengthy operation in a background thread
     new Thread(new Runnable() {
         public void run() {
                 // Update the progress bar
                 mHandler.post(new Runnable() {
                     public void run() {
                         progressDialog.setProgress(mProgressStatus);
                         Toast.makeText(getApplicationContext(), "Herererererer",Toast.LENGTH_LONG).show();
                         if(mProgressStatus == 100){                                             
                             Toast.makeText(getApplicationContext(), "Done",Toast.LENGTH_LONG).show();
                             progressDialog.dismiss();
                         }
                     }
                 });                                 
             }
     }).start(); 
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情绪操控生活 2024-11-18 00:37:00

您是否尝试过使用AsyncTask

这是文档: http://developer.android.com/reference/android/ os/AsyncTask.html

您可能想用 AsyncTask 替换线程。

Did you try to use AsyncTask ?

Here is the doc: http://developer.android.com/reference/android/os/AsyncTask.html

You may want to replace the thread with the AsyncTask.

彩虹直至黑白 2024-11-18 00:37:00

您应该使用 AsyncTask,它也称为无痛线程

现在,要在后台处理时显示进度条,下面是一个示例:

    // ASync Task Begin to perform 
 private class performBackgroundTask extends Async Task < Void, Void, Void >  
 {
    private ProgressDialog Dialog = new ProgressDialog( this);

    protected void onPreExecute() 
    {
           // here you have place code which you want to show in UI thread like progressbar or dialog or any layout . 
           Dialog.setMessage(" please wait...");
           Dialog.show();
    }

    protected Void doInBackground(Void... params) 
    {
       // write here the code to download  or to perform any background task.
   return null;
  }

  protected void onPostExecute(Void unused) {
     Dialog.dismiss();  // here we have to dismiss the progress dialog 
   // To display fetched data in textview, imageview, or any

  }

 }

You should use AsyncTask which is also known as Painless threading.

Now, to show progressbar while processing in background, here is an example:

    // ASync Task Begin to perform 
 private class performBackgroundTask extends Async Task < Void, Void, Void >  
 {
    private ProgressDialog Dialog = new ProgressDialog( this);

    protected void onPreExecute() 
    {
           // here you have place code which you want to show in UI thread like progressbar or dialog or any layout . 
           Dialog.setMessage(" please wait...");
           Dialog.show();
    }

    protected Void doInBackground(Void... params) 
    {
       // write here the code to download  or to perform any background task.
   return null;
  }

  protected void onPostExecute(Void unused) {
     Dialog.dismiss();  // here we have to dismiss the progress dialog 
   // To display fetched data in textview, imageview, or any

  }

 }
灼痛 2024-11-18 00:37:00

@Hari ..

我认为您正在从startUploading()内部调用另一个函数,这就是ProgressDialog不显示的原因。尝试在 XML 中创建进度条并将所有函数写入按钮单击内。如果这是一个递归函数,请更改您的逻辑以在按钮单击本身内调用它。

@Hari..

I think you are calling another functions from inside startUploading(),that's why ProgressDialog is not shown. Try creating the progressBar in XML and write all functions inside the button click. If that is a recursive function change your logic to call this inside the button click itself.

尸血腥色 2024-11-18 00:37:00

Paresh Mayani 上面发布的答案将不起作用,因为您无法

new ProgressDialog(this)

在 AsyncTask 中使用。 “this”需要是一个上下文。

The answer posted above by Paresh Mayani will not work since you can't use

new ProgressDialog(this)

in the AsyncTask. "this" needs to be a Context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文