2017-08-11 58 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; 
} 
} 
相關問題