简体   繁体   English

android AsyncTask错误与Progressdialog

[英]android AsyncTask error with Progressdialog

I have a problem with my progress Dialog and I can't find what Im doing wrong. 我的进度对话框有问题,我找不到我做错了什么。 When I press the button Localize, my code does the new AsyncTask and the progressDialog start, but finally it stop itself and display an error. 当我按下本地化按钮时,我的代码执行新的AsyncTask并启动progressDialog,但最后它会自行停止并显示错误。 I think the problem is because is too long what it has to do. 我认为问题是因为要做的时间太长了。 The error is this : 错误是这样的:

An error ocurred while executing doInBackground() 执行doInBackground()时发生错误

I think the problem is with publishProgress() but I dont know how and where use it. 我认为问题出在publishProgress()上,但是我不知道如何以及在哪里使用它。

Here my code: 这是我的代码:

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;    
    } 
} 

This is out of GPSLocation class, and is called from Localize.setOnclicklistener(this); 这不在GPSLocation类中,并从Localize.setOnclicklistener(this);中调用。

        @Override
    public void onClick(View v){

        if (v.getId() == R.id.localiza){

            new GPSLocation().execute();
             }
         }

This method here : 这种方法在这里:

  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);
        }

    }

Thank you. 谢谢。

Do not use the method .setText() in your doInBackground(). 不要使用该方法.setText()在doInBackground()。 It will lead to an error. 这将导致错误。 Instead you can settext in onPostExecute() 相反,您可以在onPostExecute()中设置onPostExecute()

Chnage your code to 修改您的代码以

@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;    
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM