2017-08-11 54 views
0

我试图理解Dagger2。我已经遵循了几个例子,使用一个组件/模块是有道理的,但添加一个混淆了我。我不能在我的应用程序类中使用多个组件吗?在应用程序类中使用两个Dagger组件

两个匕首组件红色突出显示,并说“无法解析符号......”

public class MyApplication extends Application { 

StorageComponent component; 
ImageDownloaderComponent imageDownloaderComponent; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    component = DaggerStorageComponent 
      .builder() 
      .storageModule(new StorageModule(this)) 
      .build(); 

    imageDownloaderComponent = DaggerImageDownloaderComponent 
      .builder() 
      .imageDownloaderModule(new ImageDownloaderModule(this)) 
      .build(); 

} 

public StorageComponent getComponent() { 
    return component; 
} 

public ImageDownloaderComponent getImageDownloaderComponent() { 
    return this.imageDownloaderComponent; 
} 
} 
+1

你可以在我的应用程序类使用一个以上的组件。尝试改变返回imageDownloaderComponent;并重建您的项目。 – eurosecom

回答

0

我结束了在一个模块中把两件(SharedPrefs和毕加索),并在我的应用程序类返回的组件。希望措辞有道理。

@Module 
public class StorageModule { 
private final MyApplication application; 

public StorageModule(MyApplication application) { 
    this.application = application; 
} 

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

@Singleton 
@Provides 
ImageDownloader provideImageDownloader() { 
    return new ImageDownloader(application); 
} 
} 

所有MyApplication类:

public class MyApplication extends Application { 

StorageComponent component; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    component = DaggerStorageComponent 
      .builder() 
      .storageModule(new StorageModule(this)) 
      .build(); 

} 

public StorageComponent getComponent() { 
    return component; 
} 
} 
相关问题