2

I'm following the example from here on how to work with background tasks in RoboAsyncTask: https://code.google.com/p/roboguice/wiki/RoboAsyncTask

However, in my code when I try to @Inject ProgressDialog dialog I get a runtime error:

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

My code is like so:

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

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

It gets called from an activity like this: new MyTask(getApplicationContext(), "some name").execute();

Sushil
  • 8,250
  • 3
  • 39
  • 71
Anthony
  • 33,838
  • 42
  • 169
  • 278

1 Answers1

0

I think, your problem is ProgressDialog class doesn't have a default constructor, and probably the other constructors aren't annotated with "@Inject".

To create a ProgressDialog, you need a Context object. First, you should create a Provider class for ProgressDialog class:

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

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

Then you have to bind the ProgressDialog.class to the provider in your RoboGuice Module class. You can find tutorial for this here: http://eclipsesource.com/blogs/2012/09/25/advanced-android-testing-with-roboguice-and-robolectric/

I didn't try the code but it shoud work.