【问题标题】:android AsyncTask error with Progressdialog带有Progressdialog的android AsyncTask错误
【发布时间】: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 元素。你必须从那里返回一些东西给onPostExecutesetText
  • 这些:longitud.setText(String.valueOf(longitude)); & latitud.setText(String.valueOf(latitude)); 是您的问题。将它们注释掉并运行,我敢打赌你会丢失错误。

标签: android android-asynctask progressdialog


【解决方案1】:

不要在 doInBackground() 中使用 .setText() 方法。这将导致错误。相反,您可以在onPostExecute()中设置文本

将您的代码更改为

@Override 
protected void onPostExecute(Void result)
{  
    pd.dismiss(); 
    longitud.setText(String.valueOf(longitude));
    latitud.setText(String.valueOf(latitude));
}

@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);
        }  
    } 
    return null;    
} 

【讨论】:

  • 我认为这是正确的,有关 OP 的更多信息,您必须在 UI 线程上运行 UI 更改。在异步任务中,它可以在 onPreExecute()、onProgressUpdate(Progress...) 和 onPostExecute(Result) 中完成
  • @Asok,我知道答案。那就是我把它贴在这里。我只是想帮助用户。但仅此而已……
  • @Ramkiran 如果是这样,那么答案已经存在,不需要。凭借您的声誉,您应该已经关闭并引用了数千个重复项之一。或为我的评论 +1。
  • @Asok,以后如果有人看到这个页面,一般访问者必须看到答案然后发布的 cmets。所以如果你知道答案发布它作为答案而不是评论
  • 答案应该发布到一个问题上,而不仅仅是 cmets @asok ...如果您认为您的评论就是答案,那么您应该已经回答了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 2012-06-15
  • 2011-10-12
  • 2015-05-12
  • 2018-01-28
  • 1970-01-01
相关资源
最近更新 更多