为什么这个AsyncTask实现没有编译?

时间:2016-04-03 01:52:45

标签: java android asynchronous android-asynctask

我正在使用此例程在另一个线程上启动永久函数,但随时停止或重新启动它。但是,我在编译(注释)时遇到两个错误:

    public class MainActivity extends AppCompatActivity 
    {
            protected void onCreate(Bundle savedInstanceState) {

        StrtBtn.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {
                    // I want to start the function on another thread here
                    myTask = new MyTask();
                    myTask.execute();
                  }});

            StpBtn.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {
                    // I want to stop the function
                    message = "StopVideo";
                    myTask.cancel(true);
                  }});

            public void MyFunction()
            {
                // whatever
            }
        }

public class MyTask extends AsyncTask<Void, Void, Void> // Error: class 'MyTask' is public, should be declared in a file named 'MyTask.Java'
{
    protected Void doInBackground(Void... params) 
    {
        while(!isCancelled())
        {
            // my code here to call the function here
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您的问题是AsyncTask声明不正确,请尝试以下代码:

public class MyTask extends AsyncTask<Void, Void, Void>{

    protected Void doInBackground(Void... params){
           // ..
           while(NotCancelled){
            // my code to call the function here
            }
        }

        return null;
    }
}

以下是您的完整示例:

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {

        StrtBtn.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {
                    // I want to start the function on another thread here
                    myTask = new MyTask();
                    myTask.execute();
                }
            });

    StpBtn.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {
                    // I want to stop the function
                    message = "StopVideo";
                    myTask.cancel(true);
                }
            });
    }

    public void MyFunction() {
        // whatever
    }


    private class MyTask extends AsyncTask<Void, Void, Void> // Error: class 'MyTask' is public, should be declared in a file named 'MyTask.Java'
    {
        protected Void doInBackground(Void... params) {
            while (!isCancelled()) {
                // my code here to call the function here
            }
            return null;
        }
    }
}

答案 1 :(得分:1)

使用Void而非void作为doInBackground的返回类型。 喜欢这个

    class MyTask extends AsyncTask<Void,Void,Void>{


    protected Void doInBackground(Void... params){
        // Your code here
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
    }

并且在调用此任务时简单

myTask.execute()

我假设您在onPostExecute

中没有对结果做任何事情