2014-02-26 37 views
2

我從這裏如何與後臺任務在RoboAsyncTask以下工作的例子:https://code.google.com/p/roboguice/wiki/RoboAsyncTask如何綁定ProgressDialog到RoboAsyncTask

然而,在我的代碼,當我嘗試@Inject ProgressDialog dialog我得到一個運行時錯誤:

Process: com.aerstone.mobile, PID: 5118 
com.google.inject.ConfigurationException: Guice configuration errors: 
1) Could not find a suitable constructor in android.app.ProgressDialog. Classes 
    must have either one (and only one) constructor annotated with @Inject or a 
    zero-argument constructor that is not private. 

    at android.app.ProgressDialog.class(Unknown Source) 

    while locating android.app.ProgressDialog 
    for field at com.aerstone.mobile.ui.task.SaveUserProfileTask.dialog(Unknown Source) 
    while locating com.aerstone.mobile.ui.task.MyTask 

我的代碼是像這樣:

public class MyTask extends RoboAsyncTask<String> { 
    @Inject protected ProgressDialog dialog; 
    protected String name; 

    public MyTask (Context context, String name) { 
     super(context); 
     this.name = name; 
    } 
    .... 
} 

它從這樣一個活動叫做:new MyTask(getApplicationContext(), "some name").execute();

回答

0

我想,你的問題是ProgressDialog類沒有默認構造函數,並且可能其他構造函數沒有用「@Inject」註釋。

要創建ProgressDialog,您需要一個Context對象。首先,你應該創建一個供應商類ProgressDialog類:

public class ProgressDialogProvider implements Provider<ProgressDialog> { 
    @Inject 
    Context context; 

    @Override 
    public ProgressDialog get() { 
     return new ProgressDialog(context); 
    } 
} 

然後,你必須到ProgressDialog.class綁定到你的RoboGuice Module類的供應商。你可以在這裏找到這個教程:http://eclipsesource.com/blogs/2012/09/25/advanced-android-testing-with-roboguice-and-robolectric/

我沒有嘗試代碼,但它應該工作。