2014-11-20 29 views
0

我的異步方法是給編譯器error.This是我的代碼:的Android異步任務編譯器錯誤

private void registerInBackground() { 
    new AsyncTask() { 
     @Override 
     protected String doInBackground(Void... params) { 

     } 

     @Override 
     protected void onPostExecute(String msg) { 

     } 
    }.execute(null, null, null); 
} 

對於線2我收到此錯誤:

Multiple markers at this line 
    - The type new AsyncTask(){} must implement the inherited abstract method AsyncTask.doInBackground(Object...) 
    - Type safety: The method execute(Object...) belongs to the raw type AsyncTask. References to generic type AsyncTask<Params,Progress,Result> should be 
    parameterized 
    - AsyncTask is a raw type. References to generic type AsyncTask<Params,Progress,Result> should be parameterized 

對於4號線我得到此錯誤:

The method doInBackground(Void...) of type new AsyncTask(){} must override or implement a supertype method 

我該如何解決它們?我的代碼有什麼問題?

回答

0

添加泛型類類型的AsyncTask構造:

private void registerDoInBackground() { 
    new AsyncTask<Void, Void, String>() { 
     @Override 
     protected String doInBackground(Void... params) { 

     } 

     @Override 
     protected void onPostExecute(String msg) { 

     } 
    }.execute(null, null, null); 
} 
0

的AsyncTask是一個參數化類,因爲你不指定參數,編譯器假定你的意思是使用Object類型作爲參數。您正在使用Void ...作爲doInBackground的參數,您應該在這種情況下使用Object []或Object ...。

@Override 
protected Object doInBackground(Object[] objects) { 
    return null; 
}