2015-04-16 30 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 
} 
相關問題