Android AsyncTask问题

时间:2010-08-20 08:11:06

标签: android android-asynctask

我有一个字符串,我想使用AsyncTask类进行更新 这是我的代码:

public class MainActivity extends Activity{

private String str = "oldString";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getString();
        }
       private void getString(){
              new CustomTask().execute();
       }

     private class CustomTask extends AsyncTask<Void, Void, String>{

          @Override
          protected String doInBackground(Void... params) {
           // TODO Auto-generated method stub
           Log.i("Lesson3", "doInBackground method");
           str = "newString";
           return "someString";
          }

          protected void onPostExecute(String s){
           Log.i("Lesson3", "onPostExecute method");

          }

}

onPostExecute方法未被调用,那就是问题??

由于

4 个答案:

答案 0 :(得分:2)

答案很简单,

@Override
protected void onPostExecute(String s){
       Log.i("Lesson3", "onPostExecute method");
}

您忘记了@Override超类的执行调用onPostExecute,因为它尚未被覆盖。

答案 1 :(得分:0)

public class MainActivity extends Activity {

    private String str = "oldString";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getString();
    }

    private void getString() {
        new CustomTask().execute();
    }

    private void postExecuteCallback() {
        Log.i("Lesson3", "String after callback: " + str );
    }

    private class CustomTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            Log.i("Lesson3", "doInBackground method");
            str = "newString";
            return "someString";
        }

        protected void onPostExecute(String s) {
            Log.i("Lesson3", "onPostExecute method");
            postExecuteCallback();
        }
    }
}

答案 2 :(得分:0)

为什么你决定不运行?以下代码工作正常(您的语法错误修复):

package com.stackoverflow;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class Test extends Activity {

    private String str = "oldString";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getString();
    }

    private void getString() {
        new CustomTask().execute();
    }

    private class CustomTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            Log.i("Lesson3", "doInBackground method");
            str = "newString";
            return "someString";
        }

        protected void onPostExecute(String s) {
            Log.i("Lesson3", "onPostExecute method");

        }

    }

}

答案 3 :(得分:0)

拥有自己的AsyncTask怎么样?

请看一下。

public abstract class CustomAsyncTask<Params, Progress, Result> {

    private boolean isFinished = false;
    private Result res;

    protected void onPreExecute() {

    }

    protected void onPostExecute(Result result) {

    }

    protected void onProgressUpdate(Progress progress) {

    }

    protected abstract Result doInBackground(Params...params);

    private void CheckAndUpdateUI()
    {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(isFinished)
                {
                    onPostExecute(res);
                }
                else
                    CheckAndUpdateUI();
            }
        },500);
    }

    public void execute(final Params...params)
    {
        onPreExecute();
        //
        CheckAndUpdateUI();
        isFinished = false;
        //
        new Thread(new Runnable() {
            @Override
            public void run() {
                res = doInBackground(params);
                isFinished = true;
            }
        }).start();
    }

}