2014-11-20 42 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; 
}