简单的Asynctask无法工作并向我抛出错误

时间:2015-04-16 06:17:41

标签: android android-asynctask

我构建了以下简单的Asynctask:

package com.example.helloworld;

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

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

    private class SimpleTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void doInBackground(Void... res) {}

    }
}

但它给了我错误:

[javac] /home/gizkp/happ/src/com/example/helloworld/HelloWorldActivity.java:17: error: HelloWorldActivity.SimpleTask is not abstract and does not override abstract method doInBackground(String...) in AsyncTask
[javac]     private class SimpleTask extends AsyncTask<String, String, String> {
[javac]             ^
[javac] /home/gizko/happ/src/com/example/helloworld/HelloWorldActivity.java:20: error: doInBackground(String...) in HelloWorldActivity.SimpleTask cannot override doInBackground(Params...) in AsyncTask
[javac]         protected void doInBackground(String... res) {}
[javac]                        ^
[javac]   return type void is not compatible with String
[javac]   where Params,Result are type-variables:
[javac]     Params extends Object declared in class AsyncTask
[javac]     Result extends Object declared in class AsyncTask
[javac] /home/gizko/happ/src/com/example/helloworld/HelloWorldActivity.java:19: error: method does not override or implement a method from a supertype
[javac]         @Override
[javac]         ^
[javac] 3 errors

我不明白这么简单的代码出了什么问题。我也尝试过改变Asynctask参数而没有任何成功。

4 个答案:

答案 0 :(得分:1)

将doInBackground的返回类型从 void 更改为 Void ,看看它是否有效

答案 1 :(得分:0)

您对void doInBackground(Void... res) {}方法的实施是错误的。

如错误所述,由于返回类型,它未能成功覆盖doInBackground方法。

试试这个

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

        @Override
        protected Void doInBackground(Void... params) {
            return null;
        }
    }

答案 2 :(得分:0)

void更改为Void,如下所示:

private class SimpleTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }
}

答案 3 :(得分:0)

尝试这个好友:更容易,效果更好:

public class RunAsyncTask extends AsyncTask<Void, Void, Boolean> {

    private static final String TAG = "RunAsyncTask";
    ProgressDialog processingDialog = ProgressDialog.show(this, "", "loading...", true);
    String responseEntity;

    protected Boolean doInBackground(Void... nothing) {
        Log.d(TAG, "doInBackground");

        HttpClient client       = new DefaultHttpClient();
        HttpPost postRequest    = new HttpPost(new String("http://api.com"));


        List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("data", "data"));             


        int responseCode = 0;
        responseEntity = "-";

        try 
          {
            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, "UTF-8");

            postRequest.setEntity(ent);
            HttpResponse responsePOST = client.execute(postRequest);

            responseCode    = responsePOST.getStatusLine().getStatusCode();
            responseEntity  = EntityUtils.toString(responsePOST.getEntity()).trim();
        } 
        catch (Exception e) { e.printStackTrace(); }


        if((200 == responseCode) && (responseEntity.length() == 0)) { return true; } 
        else { return false; }

        return true;

    }//end doInBackground

    @Override
    protected void onPostExecute(Boolean result) {
        if(null != processingDialog) { processingDialog.dismiss(); }

        if (true == result){
            Toast.makeText(getApplicationContext(), "true", Toast.LENGTH_SHORT).show();//                
        } else{ Toast.makeText(getApplicationContext(), "false", Toast.LENGTH_SHORT).show();  }


        textResult.setText("");
        textResult.setText(formatString(responseEntity));


    }//end onPostExecute
}