【发布时间】:2012-11-21 01:03:51
【问题描述】:
我的进度对话框有问题,我找不到我做错了什么。 当我按下 Localize 按钮时,我的代码会启动新的 AsyncTask 和 progressDialog,但最终它会自行停止并显示错误。我认为问题在于它必须做的事情太长了。错误是这样的:
执行doInBackground()时出错
我认为问题出在 publishProgress() 但我不知道如何以及在哪里使用它。
这是我的代码:
public class GPSLocation extends AsyncTask<Void, Void, Void>
{
boolean running =true;
@Override
protected void onPreExecute()
{
super.onPreExecute();
pd = new ProgressDialog(Configuracion.this);
pd.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
pd.cancel();
}
});
longitude=0;
latitude =0;
getLonLat();
pd.setCancelable(true);
pd.setMessage("Getting GPS Location...");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
pd.setProgress(1);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
@Override
protected void onPostExecute(Void result)
{
pd.dismiss();
}
@Override
protected Void doInBackground(Void... params) {
boolean isDataSubmitted = false;
while(!isDataSubmitted)
{
if(longitude !=0 && latitude!=0)
{
isDataSubmitted = true;
Log.d("LONGITUD", ""+longitude);
Log.d("LATITUDE", ""+latitude);
longitud.setText(String.valueOf(longitude));
latitud.setText(String.valueOf(latitude));
}
}
return null;
}
}
这是在 GPSLocation 类之外,从 Localize.setOnclicklistener(this) 调用;
@Override
public void onClick(View v){
if (v.getId() == R.id.localiza){
new GPSLocation().execute();
}
}
这个方法在这里:
public void getLonLat(){
LocationManager milocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener milocListener = new MiLocationListener();
if (milocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
milocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,milocListener);
}else if (milocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
milocManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, milocListener);
}
}
谢谢。
【问题讨论】:
-
“我的进度对话框有问题”有什么问题?
-
您无法在
doInBackground等后台线程中更改任何 UI 元素。你必须从那里返回一些东西给onPostExecute和setText。 -
这些:
longitud.setText(String.valueOf(longitude));&latitud.setText(String.valueOf(latitude));是您的问题。将它们注释掉并运行,我敢打赌你会丢失错误。
标签: android android-asynctask progressdialog