C# Xamarin - 关闭 ProgressDialog 时应用程序崩溃
C# Xamarin - Application crashes, when dismiss ProgressDialog
我正在开发 android Xamarin 应用程序。我的应用程序连接到我的网站,因为它持续时间很长,所以我使用 ProgressDialog
。但是当 ProgressDialog
关闭时,应用程序崩溃。我不明白为什么。我试图捕获错误,但应用程序也崩溃了。 (对不起我的英语)
代码如下:
ProgressDialog load;
void FTPConnection()
{
load = new ProgressDialog(this);
load.SetTitle("Načítání");
load.SetMessage("Spojování s ftp://kolemzeme.wz.cz, může to chvíli trvat...");
load.SetCancelable(false);
load.SetProgressStyle(ProgressDialogStyle.Spinner);
load.Show();
new Thread(new ThreadStart(async delegate
{
await Task.Delay(1000);
try
{
using (WebClient client = new WebClient())
{
Stream stream = client.OpenRead("http://" + web);
stream.Close();
}
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + web + "/cesta.txt");
request.Credentials = new NetworkCredential(web, pass);
request.UseBinary = true;
request.UsePassive = true;
request.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse respose = (FtpWebResponse)request.GetResponse();
RunOnUiThread(() =>
{
load.Dismiss();
});
NaCeste();
}
catch (WebException ex)
{
RunOnUiThread(() =>
{
load.Dismiss();
});
FtpWebResponse response = (FtpWebResponse)ex.Response;
SetContentView(Resource.Layout.Cesta);
Cesta();
}
}
catch (Exception ex)
{
RunOnUiThread(() =>
{
load.Dismiss();
});
load.Dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle("Nelze se připojit na FTP!")
.SetMessage("Aplikace nemůže navázat přístup k serveru FTP: http://kolemzeme.wz.cz\n\nPravděpodobně nemáte internet, nebo je server nyní nedostupný. Zkuste to později.\n\nError: " + ex.Message)
.SetCancelable(false)
.SetPositiveButton("Připojit se k wifi", delegate
{
Intent i = new Intent(Android.Provider.Settings.ActionWifiSettings);
StartActivityForResult(i, 0);
FTPConnection();
})
.SetNeutralButton("Opakovat", delegate
{
FTPConnection();
})
.SetNegativeButton("Odejít", delegate
{
Finish();
})
.Show();
}
})).Start();
}
错误的屏幕截图 here。
RunOnUiThread(() =>
{
load.Dismiss();
});
load.Dismiss();
您在 UI 线程上有一个 Dismiss
调用 运行,一个没有。您需要删除第二个。
更好的方法是将异常处理的 所有 移动到 UI 线程。所以改变:
RunOnUiThread(() =>
{
load.Dismiss();
});
FtpWebResponse response = (FtpWebResponse)ex.Response;
SetContentView(Resource.Layout.Cesta);
Cesta();
至:
RunOnUiThread(() =>
{
load.Dismiss();
FtpWebResponse response = (FtpWebResponse)ex.Response;
SetContentView(Resource.Layout.Cesta);
Cesta();
});
(例如 - 您需要对所有 catch 块进行这种样式的更改)
我正在开发 android Xamarin 应用程序。我的应用程序连接到我的网站,因为它持续时间很长,所以我使用 ProgressDialog
。但是当 ProgressDialog
关闭时,应用程序崩溃。我不明白为什么。我试图捕获错误,但应用程序也崩溃了。 (对不起我的英语)
代码如下:
ProgressDialog load;
void FTPConnection()
{
load = new ProgressDialog(this);
load.SetTitle("Načítání");
load.SetMessage("Spojování s ftp://kolemzeme.wz.cz, může to chvíli trvat...");
load.SetCancelable(false);
load.SetProgressStyle(ProgressDialogStyle.Spinner);
load.Show();
new Thread(new ThreadStart(async delegate
{
await Task.Delay(1000);
try
{
using (WebClient client = new WebClient())
{
Stream stream = client.OpenRead("http://" + web);
stream.Close();
}
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + web + "/cesta.txt");
request.Credentials = new NetworkCredential(web, pass);
request.UseBinary = true;
request.UsePassive = true;
request.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse respose = (FtpWebResponse)request.GetResponse();
RunOnUiThread(() =>
{
load.Dismiss();
});
NaCeste();
}
catch (WebException ex)
{
RunOnUiThread(() =>
{
load.Dismiss();
});
FtpWebResponse response = (FtpWebResponse)ex.Response;
SetContentView(Resource.Layout.Cesta);
Cesta();
}
}
catch (Exception ex)
{
RunOnUiThread(() =>
{
load.Dismiss();
});
load.Dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle("Nelze se připojit na FTP!")
.SetMessage("Aplikace nemůže navázat přístup k serveru FTP: http://kolemzeme.wz.cz\n\nPravděpodobně nemáte internet, nebo je server nyní nedostupný. Zkuste to později.\n\nError: " + ex.Message)
.SetCancelable(false)
.SetPositiveButton("Připojit se k wifi", delegate
{
Intent i = new Intent(Android.Provider.Settings.ActionWifiSettings);
StartActivityForResult(i, 0);
FTPConnection();
})
.SetNeutralButton("Opakovat", delegate
{
FTPConnection();
})
.SetNegativeButton("Odejít", delegate
{
Finish();
})
.Show();
}
})).Start();
}
错误的屏幕截图 here。
RunOnUiThread(() =>
{
load.Dismiss();
});
load.Dismiss();
您在 UI 线程上有一个 Dismiss
调用 运行,一个没有。您需要删除第二个。
更好的方法是将异常处理的 所有 移动到 UI 线程。所以改变:
RunOnUiThread(() =>
{
load.Dismiss();
});
FtpWebResponse response = (FtpWebResponse)ex.Response;
SetContentView(Resource.Layout.Cesta);
Cesta();
至:
RunOnUiThread(() =>
{
load.Dismiss();
FtpWebResponse response = (FtpWebResponse)ex.Response;
SetContentView(Resource.Layout.Cesta);
Cesta();
});
(例如 - 您需要对所有 catch 块进行这种样式的更改)