2015-08-26 23 views
5

olmadan sağlanamaz. Dagger 2 için tamamen yeni ve küçük bir sorunum var. bana yardım edebilir umut :) Ben benim android projesiHançer2 hatası: @Inject constructor

  1. App
  2. AppComponent
  3. AppModule
  4. MainActivity
  5. MainComponent
  6. MainModule
  7. IntentStarter
  8. aşağıdaki sınıfları
/ Şimdi:

yeniden inşa At/derleme i ben ... yapıcı olmayan IntentStarter sınıfında .. kurucular ile denedim ... varyantların ancak başarılı olamadı çok çalıştı

Error:(15, 10) error: xyz.IntentStarter cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. 
xyz..MainActivity.intentStarter 
[injected field of type: xyz..IntentStarter intentStarter] 

hatayı olsun kodlar ...

// AppComponent.class 
@Singleton 
@Component(modules = {AppModule.class}) 
public interface AppComponent { 
// Empty... 
} 

...

// AppModule.class 
@Singleton 
@Module 
public class AppModule { 

    Application application; 
    Context context; 


    public AppModule(Application app) { 
     this.application = app; 
     this.context = app.getApplicationContext(); 
    } 


    @Provides 
    public Application provideApplication() { 
     return application; 
    } 

    @Provides 
    public Context provideContext() { 
     return context; 
    } 

    @Provides 
    public EventBus provideEventBus() { 
     return EventBus.getDefault(); 
    } 

    @Provides 
    public IntentStarter provideIntentStarter() { 
     return new IntentStarter(); 
    } 
} 

...

// App.class 
public class App extends Application { 

    public AppComponent appComponent; 

    public AppComponent getAppComponent() { 
     return appComponent; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     appComponent = DaggerAppComponent.builder() 
      .appModule(new AppModule(this)) 
      .build(); 
    } 
} 

...

//MainAcitivty.class 
public class MainActivity extends MosbyActivity { 

    @Inject 
    IntentStarter intentStarter; 

    MainActivityComponent component; 

    @Override 
    protected void injectDependencies() { 
     component = DaggerMainActivityComponent.builder() 
       .appComponent(((App) getApplication()).getAppComponent()) 
       .build(); 
     component.inject(this); 
    } 
} 

...

//MainActivityComponent.class 
@ActivityScope 
@Component(dependencies = {AppComponent.class}) 
public interface MainActivityComponent { 
    void inject(MainActivity mainActivity); 
} 

...

// MainActivityModule 
@Module 
public class MainActivityModule { 

} 

...

//IntentStarter 
public class IntentStarter { 

    @Inject 
    Context context; 

} 
+0

Kişisel hüküm yöntemleri AppComponent eksik, belirli olması. – EpicPandaForce

+0

Ne demek istiyorsun? Benim AppComponent benim AppModule için işaret ve IntentStarter – Tobias

+1

için bir yöntem var gerçekten Hançer 2 içine değilim, ama belki bu yardımcı olur? http://siphon9.net/loune/2015/04/dagger-2-0-android-migration-tips/ (Hatanızı bulmak için aşağıya bakın - bileşeninize bir 'offerIntentStarter 'yöntemi eklemeyi deneyin) . –

cevap

4

İlk söylediğiniz gibi, öneri yöntemleriniz bileşenlerden eksik. Örneğin,

@Component(modules={AppModule.class}) 
@Singleton 
public interface AppComponent { 
     Context context(); 
     IntentStarter intentStarter(); 
} 

@Component(dependencies={AppComponent.class}), modules={MainActivityModule.class}) 
@ActivityScope 
public interface MainActivityComponent extends AppComponent { 
     //other provision methods 
} 

Ve saha enjeksiyonu ile bir hata yapıyoruz, sizin IntentStarter appComponent.inject(this) aramak ya ihtiyacı veya yapıcı parametre içinde bağlamı almak gerekiyor.

Ayrıca, bir kapsamlı sağlayıcı almak için @Provides açıklamalı yönteminin @Singleton kapsamına gereksinim duyduğunu ve modülün kendisini işaretlemesinin aslında hiçbir şey yapmadığını düşünüyorum.

Yani,

@Singleton 
@Component(modules = {AppModule.class}) 
    public interface AppComponent { 
    Application application(); 
    Context provideContext(); 
    EventBus provideEventBus(); 
    IntentStarter provideIntentStarter(); 
} 

@Module 
public class AppModule { 
    Application application; 
    Context context; 

    public AppModule(Application app) { 
     this.application = app; 
     this.context = app.getApplicationContext(); 
    } 


    @Provides 
    public Application provideApplication() { 
     return application; 
    } 

    @Provides 
    public Context provideContext() { 
     return context; 
    } 

    @Provides 
    @Singleton 
    public EventBus provideEventBus() { 
     return EventBus.getDefault(); 
    } 

    @Provides 
    @Singleton 
    public IntentStarter provideIntentStarter(Context context) { 
     return new IntentStarter(context); 
    } 
} 

@ActivityScope 
@Component(dependencies = {AppComponent.class}, modules={MainActivityModule.class}) 
public interface MainActivityComponent extends AppComponent { 
    void inject(MainActivity mainActivity); 
} 

public class IntentStarter {  
    private Context context; 

    public IntentStarter(Context context) { 
     this.context = context; 
    } 
} 
İlgili konular