Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

ProgressDialog doesnt show | Threading problems.

In the following code, i have a problem at mProgressDialog = new ProgressDialog(Activity);, saying "Can't create handler inside thread that has not called Looper.prepare()" I understant where the problem is but i am not entirely sure how to implement the new thread in the following class.

` public sealed class PreferencesFragment : PreferenceFragment, ISharedPreferencesOnSharedPreferenceChangeListener, IOnPreferenceClickListener
{
private static readonly string TAG = "PreferencesFragment";

        private ProgressDialog mProgressDialog;
        private LogCollector mLogCollector;

        private void runOnClick()
        {
            onPreExecute();
            mLogCollector = new LogCollector(Activity);
            onPostExecute(false);
        }

        private void onPreExecute()
        {
                mProgressDialog = new ProgressDialog(Activity);

                mProgressDialog.OwnerActivity = Activity;

                mProgressDialog.SetMessage(Activity.GetString(Resource.String.progress_collecting_log));

                mProgressDialog.Indeterminate = true;

                mProgressDialog.SetCancelable(false);

                mProgressDialog.Show();
            }

        private void onPostExecute(Boolean result)
        {
            try
            {
                mProgressDialog.Dismiss();
            }
            catch (System.Exception e)
            {
                if (CTC.DEBUG)
                {
                    Log.Warn(TAG, e.Message);
                }
            }

            if (result)
            {
                String emailTo = Activity.GetString(Resource.String.diagnostic_email_to);
                String emailSubject = Activity.GetString(Resource.String.diagnostic_email_subject);
                mLogCollector.sendLog(emailTo, emailSubject, emailSubject);
            }
        }


        public void OnSharedPreferenceChanged(ISharedPreferences sharedPreferences, string key)
      {            
       }

        public bool OnPreferenceClick(Preference preference)
        {
            Thread preference_click_thread = new Thread(() =>
            {
                runOnClick();
            });

            preference_click_thread.Start();

            if (CTC.DEBUG)
            {
                // Statistics click listener
                Preference statisticsPreference = FindPreference(PreferencesActivity.KEY_STATISTICS);

            }
            return true;
        }

        public bool OnPreferenceClickListener(Preference preference)
        {
            string key = preference.Key;
            Intent intent = new Intent(Intent.ActionView);
            intent.AddFlags(Android.Content.ActivityFlags.ClearWhenTaskReset);
            Activity.StartActivity(typeof(StatisticsActivity));
            return true;
        }
    }`

Regards

Sign In or Register to comment.