2017-11-14 16 views
0

私はAppWidgetProviderでDagger2を使用しようとしています。 しかし、私の豆は短剣で決して注入されません。AppWidgetProvider with dagger2

私はサンプルからスタート:https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample

マイAppModule:

@Module 
public class AppModule { 

    @Provides 
    @Singleton 
    Context providesApplicationContext(Application application) { 
     return application; 
    } 

    @Singleton @Provides 
    MyDb provideDb(Application app) { 
     return Room.databaseBuilder(app, MyDb.class,"my.db") 
       .build(); 
    } 

    @Provides 
    @Singleton 
    SharedPreferences providesSharedPreferences(Application application) { 
     return PreferenceManager.getDefaultSharedPreferences(application); 
    } 
} 

マイWeatherModule

@Module 
public class WeatherModule { 

    @Singleton @Provides 
    public OpenWeatherMapService provideOpenWeatherMapService() { 
     return new Retrofit.Builder() 
       .baseUrl(BuildConfig.OPEN_WEATHER_MAP_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .addCallAdapterFactory(new LiveDataCallAdapterFactory()) 
       .build() 
       .create(OpenWeatherMapService.class); 
    } 

    @Singleton @Provides 
    public WeatherDao provideWeatherDao(MyDb db) { 
     return db.weatherDao(); 
    } 
} 

マイウィジェットダガーコンポーネント:

@Singleton 
@Component(modules = {AppModule.class, WeatherModule.class}) 
public interface WeatherWidgetComponent { 
    void inject(Context context); 
} 

マイアプリケーション:

public class MyApplication extends Application implements HasActivityInjector { 

    @Inject 
    DispatchingAndroidInjector<Activity> dispatchingAndroidInjector; 

    private WeatherWidgetComponent weatherWidgetComponent; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     AppInjector.init(this); 

     weatherWidgetComponent = DaggerWeatherWidgetComponent.builder() 
       .build(); 
    } 

    public WeatherWidgetComponent getWeatherWidgetComponent() { 
     return weatherWidgetComponent; 
    } 

    @Override 
    public DispatchingAndroidInjector<Activity> activityInjector() { 
     return dispatchingAndroidInjector; 
    } 
} 

マイAppWidgetProvider logcatで

public class WeatherAppWidgetProvider extends AppWidgetProvider { 


    @Inject 
    WeatherDao weatherDao; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     super.onReceive(context, intent); 
     inject(context); 
    } 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 
     inject(context); 

     for (int widgetId : appWidgetIds) { 
      updateUI(appWidgetManager, context, widgetId); 
     } 
    } 

    @Override 
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { 
     super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions); 
     inject(context); 
     updateUI(appWidgetManager, context, appWidgetId); 
    } 

    @Override 
    public void onEnabled(Context context) { 
     super.onEnabled(context); 
     inject(context); 
    } 

    private void inject(Context context){ 
     Timber.d("inject"); 
     if(weatherDao ==null) { 
      Timber.d("null so try to inject"); 
      ((MyApplication) context.getApplicationContext()).getWeatherWidgetComponent().inject(context); 
     } 
    } 

    private void updateUI(AppWidgetManager appWidgetManager, Context context, int appWidgetId){ 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), 
       R.layout.weather_widget); 

     if(weatherDao ==null){ 
      Timber.d("weatherDao null"); 
     } 
    } 
} 

私はいつも持っている:

11-14 18:41:43.494 3812 3812 D WeatherAppWidgetProvide:注入

11-14 18:41:43.495 3812 3812 D WeatherAppWidgetProvide:nullだから注入しよう

11-14 18:41:43.198 3812 3812 D WeatherAppWidgetProvide:weatherDaoヌル

+1

基本クラス(コンテキスト) – EpicPandaForce

答えて

0

ガボールはコメントで指摘したように、あなたはフィールド注入のための具体的なクラスを指定する必要があります。

@Singleton 
@Component(modules = {AppModule.class, WeatherModule.class}) 
public interface WeatherWidgetComponent { 
    void inject(Context context); 
} 

Contextがどの@Inject注釈付きのフィールドを持っていないので、何もしませんContextを注入します。生成されたコードは、基本的にコンテキストを挿入して何もしません。

あなたはダガーが適切WeatherAppWidgetProviderを注入すると.inject(this)を呼び出すと、実際に何かをするコードを生成しますvoid inject(WeatherAppWidgetProvider provider)void inject(Context context)への呼び出しを交換した場合。

+0

だけでなく、フィールド注入の具体的なクラスを指定する必要があります。それは私のAppWidgetProviderでコンポーネントを作成する "クリーン"ですか?私は複数のAppWidgetProvidersを持っているので、私はアプリケーションクラスを軽く保つことを好みます。私はコンポーネントを作成し、私のAppWidgetProvidersのインスタンスを挿入するBaseAppWidgetProvidersを作成したいと思います。 – LaurentY

関連する問題