2013-02-26 55 views
1

我試圖在twitter oauth活動中實現asynctask,但是我得到了2個錯誤。我希望asynctask返回一個消費者對象。android asynctask attempt

這裏是我的代碼:

class getCommonsHttpConsumer extends AsyncTask<Void, Void, Void> { 

@Override 
protected OAuthConsumer doInBackground() { 

    return new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET); 
} 
} 

我得到兩個錯誤

  1. getCommonsHttpConsumer:類型getCommonsHttpConsumer必須實現繼承的抽象方法AsyncTask.doInBackground(無效...)

  2. doInBackground():getCommonsHttpConsumer類型的doInBackground()方法必須覆蓋或實現超類型方法

我做錯了什麼?

+0

如果我們的答案有幫助,你應該接受最好的答案。如果不是,你可以隨時留下反饋。 – 2013-03-09 04:02:34

回答

1

如果你考慮的AsyncTask的文檔,你會看到這一點: -

AsyncTask<Params, Progress, Result> 

因爲,你已經給你​​因爲這樣,你的

protected OAuthConsumer doInBackground() 

拋出這些錯誤。

更改爲Void返回類型的doInBackground()AsyncTaskAsyncTask<Void, Void, OAuthConsumer>來修復它。

1

閱讀編譯器告訴你什麼,並熟悉文檔。您需要更改類聲明的參數來聲明doInBackground將返回OAuthConsumer也有doInBackground()接受Void...(可變參數):

class getCommonsHttpConsumer extends AsyncTask<Void, Void, OAuthConsumer> { 

@Override 
protected OAuthConsumer doInBackground(Void... params) { 

也是一個很好的解釋(除了文檔),可以發現here

0

這裏是做什麼:更多細節

class getCommonsHttpConsumer extends AsyncTask<Void, Void, OAuthConsumer> { 

    @Override 
    protected OAuthConsumer doInBackground() { 

     return new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET); 
    } 

    @Override 
    protected void onPostExecute(OAuthConsumer result) { 
     // process result 
    } 
} 

檢查documentation