MissMe icon indicating copy to clipboard operation
MissMe copied to clipboard

what am i doing wrong

Open sunirmalya opened this issue 5 years ago • 1 comments

hi love this - when i use it - it is closing after reaching 100 but also closing whole app - what am i doing wrong please?

public void doProgressDialog() { ProgressDialog progressDialog = new ProgressDialog(this); // Set horizontal progress bar style. progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // Set progress dialog message. progressDialog.setMessage("This is a horizontal progress dialog."); // The maxima progress value. progressDialog.setMax(100); // Whether progress dialog can be canceled or not. progressDialog.setCancelable(true); // Popup the progress dialog. progressDialog.show(); // Create a new thread object. Thread thread = new Thread(new Runnable() {

		@Override
		public void run() {
			int i = 0;
			// Update progress bar every 0.3 second.
			while (i < 100) {
				try {
					Thread.sleep(30);
					// Update the progress value.
					progressDialog.incrementProgressBy(1);
					// Update the secondary progress value.
					progressDialog.incrementSecondaryProgressBy(5);
					i++;
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
			// Close and delete the dialog when the progress bar is finished
			progressDialog.dismiss();
		}
	});

	// Start the thread.
	thread.start();

}

sunirmalya avatar Aug 10 '19 00:08 sunirmalya

Hi. You are trying access progressDialog from the background thread. You cannot access UI elements from the background thread in Android. This is causing the crash in your case. Use Handler to access progressDialog from your thread.

This StackOverflow post might help: https://stackoverflow.com/questions/11123621/running-code-in-main-thread-from-another-thread

Livin21 avatar Aug 12 '19 06:08 Livin21