Android ProgressDialog. show (), unable to display the waiting box, Android progressdialog

Source: Internet
Author: User

Android ProgressDialog. show (), unable to display the waiting box, Android progressdialog

First, I will post out my solution code:

Package com. park;


Import java. io. IOException;
Import java. io. InputStream;
Import java. lang. reflect. InvocationTargetException;
Import java. lang. reflect. Method;
Import java. util. Objects;
Import java. util. Timer;
Import java. util. TimerTask;
Import java. util. UUID;

Import android. app. Activity;
Import android. app. ProgressDialog;
Import android. content. Intent;
Import android. OS. AsyncTask;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. OS. ParcelUuid;
Import android. text. format. Time;
Import android. util. Log;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. Window;
Import android. webkit. lelemessage. MessageLevel;
Import android. widget. ImageView;
Import android. widget. TextView;
Import android. bluetooth .*;

Import android. widget. Toast;

/**

* Created by lenovo on 2015/5/25.
*/
Public class CommunityParking extends Activity implements View. OnClickListener {

Private ProgressDialog progressDialog, progressDialog2, progressDialog3;
Private int parkNum = 50;
Private ImageView back, around_parking, zero, ten, oneHour, threeHour;
Private Intent intent;
Private TextView num;
Private Handler handler;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
RequestWindowFeature (Window. FEATURE_NO_TITLE );
SetContentView (R. layout. community_parking );
Num = (TextView) findViewById (R. id. num );
Int a = 10;
Back = (ImageView) findViewById (R. id. back );
Around_parking = (ImageView) findViewById (R. id. around_parking );
Back. setOnClickListener (this );
Around_parking.setOnClickListener (this );
Timer timer = new Timer ();
TimerTask timerTask = new TimerTask (){

@ Override
Public void run (){
// TODO Auto-generated method stub
RunOnUiThread (new Runnable (){
Public void run (){
ParkNum ++;
Num. setText ("" + parkNum );
ProgressDialog. dismiss ();
}
});
}
};
ProgressDialog = ProgressDialog. show (this, "", "loading data. Please wait ...");
Timer. schedule (timerTask, 4000,400 0 );
Zero = (ImageView) findViewById (R. id. zero );
Ten = (ImageView) findViewById (R. id. ten );
OneHour = (ImageView) findViewById (R. id. oneHour );
OneHour. setOnClickListener (new test ());
ThreeHour = (ImageView) findViewById (R. id. threeHour );

ThreeHour. setOnClickListener (new test2 ());

}

@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. back:
Intent = new Intent (this, MainActivity. class );
StartActivity (intent );
Break;
Case R. id. around_parking:
Intent = new Intent ();
Intent. setAction ("android. intent. action. AROUPARK ");
StartActivity (intent );
Break;
}
}

Class test implements OnClickListener {

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
ProgressDialog2 = ProgressDialog. show (CommunityParking. this, "", "estimated parking space. Please wait ...");
Handler handler = new Handler ();
Handler. postDelayed (new Runnable (){
Public void run (){
ProgressDialog2.dismiss ();
Ten. setVisibility (View. VISIBLE );
}
},1000 );
}
}

Class test2 implements OnClickListener {

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
ProgressDialog3 = ProgressDialog. show (CommunityParking. this, "", "estimated parking space. Please wait ...");
Handler handler = new Handler ();
Handler. postDelayed (new Runnable (){
Public void run (){
ProgressDialog3.dismiss ();
Zero. setVisibility (View. VISIBLE );
}
},1000 );
}
}

}

Yes. The progressdialog is executed in oncreate. show (); can display the progress box normally, but when I set a click event for an imageview in the xml page, call progressdialog. show () cannot display the progress box. At the beginning, I defined click events as follows. I asked CommunityParking implements Onclicklistner to process click monitoring events in a unified manner, but I found that this method could not display the waiting dialog box, then I use new test () to create a listener class for click events, and then the wait dialog box is displayed. I still don't know why, but the problem has been solved.

Another point is the thread I used at the beginning. sleep (). pause the thread for two seconds, but this will always lead to errors, and the waiting box cannot be displayed. Later I searched stackoverflow, which said to use handler to process the program waiting for the problem and finally solve the problem. For details, refer to my code and leave a message if you have any questions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.