Failed to run an AsyncTask

0

I have the following error when using AsyncTask

  

method gettext must be called from the thread

My code:

private class EnviarCorte extends AsyncTask<Void, Void, Void>{
    public void onPreExecute(){
        //Antes de empezar
        Precarga.setVisibility(View.VISIBLE);
    }

    public void onPostExecute(Void unused){
        //Al finalizar
        finish();
    }
    @Override
    protected Void doInBackground(Void... params){
        SQLiteHelper basededatos = new SQLiteHelper(getApplicationContext(), null, null, 1);
        SQLiteDatabase bd = basededatos.getWritableDatabase();

        registro.put("fecha", Fecha_actual.format(new Date()));
        registro.put("folio", Folio.getText().toString());
        registro.put("ubicacion", Ubicacion.getText().toString());
        registro.put("productor", Productor.getText().toString());
        registro.put("telefono", Telefono.getText().toString());
        registro.put("celular", Celular.getText().toString());
        registro.put("fruta", Fruta.getText().toString());
        registro.put("corte", TipoCorte);
        registro.put("caracteristicas", Caracteristicas.getText().toString());
        registro.put("estado", "1");

        //Operaciones
        return null;
    }
}

.

ContentValues registro = new ContentValues();
EditText Folio,Ubicacion,Productor,Telefono,Celular,Fruta,Caracteristicas;  

Method onCreate() :

Folio = (EditText)findViewById(R.id.regFolio);
        Ubicacion = (EditText)findViewById(R.id.regUbicacion);
        Productor = (EditText)findViewById(R.id.regProductor);
        Telefono = (EditText)findViewById(R.id.regFijo);
        Celular = (EditText)findViewById(R.id.regCelular);
        Fruta = (EditText)findViewById(R.id.regFruta);
        Caracteristicas = (EditText)findViewById(R.id.regCaracteristicas);
  

Solution

EnviarCorte nuevocorte = new EnviarCorte();
            nuevocorte.execute(Fecha_actual.format(new Date()),
                                Folio.getText().toString(),Ubicacion.getText().toString(),
                                Productor.getText().toString(),Telefono.getText().toString(),
                                Celular.getText().toString(),Fruta.getText().toString(),
                                TipoCorte,Caracteristicas.getText().toString(),"0");

@Override
    protected Void doInBackground(String... params) {
        SQLiteHelper basededatos = new SQLiteHelper(getApplicationContext(), null, null, 1);
        SQLiteDatabase bd = basededatos.getWritableDatabase();

        registro.put("fecha", params[0]);
        registro.put("folio", params[1]);
        registro.put("ubicacion",params[2]);
        registro.put("productor", params[3]);
        registro.put("telefono", params[4]);
        registro.put("celular", params[5]);
        registro.put("fruta", params[6]);
        registro.put("corte", params[7]);
        registro.put("caracteristicas", params[8]);
        registro.put("estado",params[9]);

        bd.insert(basededatos.TABLE_CORTES, null, registro);
        bd.close();
        return null;
    }
    
asked by DoubleM 23.01.2017 в 21:01
source

1 answer

0

According to your error:

  

method gettext must be called from the thread

The problem is that operations are performed on the main thread when trying to modify the UI from the doInBackground() method, calling the getText() method, which is incorrect,

 @Override
    protected Void doInBackground(Void... params){
        SQLiteHelper basededatos = new SQLiteHelper(getApplicationContext(), null, null, 1);
        SQLiteDatabase bd = basededatos.getWritableDatabase();

        registro.put("fecha", Fecha_actual.format(new Date()));
        registro.put("folio", Folio.getText().toString());
        registro.put("ubicacion", Ubicacion.getText().toString());
        registro.put("productor", Productor.getText().toString());
        registro.put("telefono", Telefono.getText().toString());
        registro.put("celular", Celular.getText().toString());
        registro.put("fruta", Fruta.getText().toString());
        registro.put("corte", TipoCorte);
        registro.put("caracteristicas", Caracteristicas.getText().toString());
        registro.put("estado", "1");

        //Operaciones
        return null;
    }

you have to do it when the execution of Asynctask, ends in the onPostExecute( method), which is where you can perform operations on the main thread:

   @Override
    protected void onPostExecute(String result) {

            registro.put("fecha", Fecha_actual.format(new Date()));
            registro.put("folio", Folio.getText().toString());
            registro.put("ubicacion", Ubicacion.getText().toString());
            registro.put("productor", Productor.getText().toString());
            registro.put("telefono", Telefono.getText().toString());
            registro.put("celular", Celular.getText().toString());
            registro.put("fruta", Fruta.getText().toString());
            registro.put("corte", TipoCorte);
            registro.put("caracteristicas", Caracteristicas.getText().toString());
            registro.put("estado", "1");

    }
    
answered by 24.01.2017 в 01:40