0

当我在结果活动中检索值时。我什么也没得到,我猜该值没有从doInBackgroundto传递onPostExecute。知道有什么问题吗?还是我以错误的方式传递它

 class calculateTask extends AsyncTask<String, String, String>
 {
      @Override
        protected String doInBackground(String... params) {
             Thread t= new Thread();
                try {
                    t.sleep(10000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            int pix=0;
            int circ=0;
            int width1=mBitmap.getWidth();
            int height1=mBitmap.getHeight();

            for(int i=0;i<width1;i++)
            {
            for(int j=0;j<height1;j++)
            {

            if(mBitmap.getPixel(i, j)==Color.WHITE)
            {

                pix++;
            }

            if(mBitmap.getPixel(i, j)==Color.LTGRAY)
            {
                circ++;
            }
            }
            }
               int percentage=100-((pix-circ))*100  ;
            String p=intToChar(array,percentage);



            return p;

        }
        @Override
        protected void onPostExecute(String p) {


        Intent i= new Intent(circle1.this,results.class);

        i.putExtra("perc", p);

        startActivity(i);

            //super.onPostExecute(result);
        }

    }
    public String intToChar(char[] array, int pix) {
        // TODO Auto-generated method stub
         String b="";
            int i = array.length - 1;
            while (pix > 0 && i >= 0) {
                array[i--] = (char) (48 + pix % 10);
                pix /= 10;
            }     b = new String(array);

            return b;
    }
4

1 回答 1

0

我会使用一个全局变量。然后在doInBackground中给全局变量赋值,然后在onPostExecute中取回。

编辑:很多事情都是错误的。

这是我的代码示例之一

private class Load extends AsyncTask<Void, Void, Void>
    {
    private ProgressDialog progressDialog;
    private Context context;
    private boolean internet, refresh;

    public Load(Context context, boolean internet, boolean refresh)
    {
        this.internet = internet;
        this.refresh = refresh;
        this.context = context;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        progressDialog = ProgressDialog.show(context, null, "Loading data ...");
    }

    @Override
    protected Void doInBackground(Void... voids)
    {
        taskComplete = false;
        while (!taskComplete)
        {
            getData(this.internet);
            try
            {
                Thread.sleep( 1000 );
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);
        populateData();
        progressDialog.dismiss();
    }
}

试着模仿这个。getData 完成后将 taskComplete 设置为 true。

于 2013-08-06T18:21:26.563 回答