2015-04-16 31 views
0

我已经建立了以下简单的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参数而没有任何成功。

回答

1

变化从doInBackground的返回类型无效虚空,看看它是否工作

+0

它还挺作品,但仍然抛出错误对我。为了在没有编译错误的情况下工作,我必须得到回报。 –

+0

@gizko是当然,你可以返回null,它会工作,然后 –

0

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

正如错误中提到的,由于返回类型,它没有成功覆盖doInBackground方法。

试试这个

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

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

变化voidVoid如下:

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

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

它的作品,如果我把回报呢!我认为回报没有必要。谢谢! –

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 
} 
相关问题