2015-08-26 20 views
5

प्रदान नहीं कर सकता मैं डैगर 2 के लिए बिल्कुल नया हूं और एक छोटी सी समस्या है। क्या आप मेरी मदद कर सकते हैं उम्मीद :) मेरे पास मेरी एंड्रॉयड परियोजनाडैगर 2 त्रुटि: बिना @ इंजेक्टर कन्स्ट्रक्टर

  1. अनुप्रयोग
  2. AppComponent
  3. AppModule
  4. MainActivity
  5. MainComponent
  6. MainModule
  7. IntentStarter
  8. में निम्नलिखित वर्गों
/ अब:

पुनर्निर्माण पर/संकलन मैं

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] 

मैं वेरिएंट की लेकिन सफलता नहीं मिली बहुत कोशिश की ... मैं IntentStarter वर्ग .. में एक निर्माता के साथ इसे करने की कोशिश निर्माता के बिना ... त्रुटि मिलती है कुछ कोड ...

// 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(); 
    } 
} 

...

0,123,
// 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

आपका प्रावधान तरीकों AppComponent से याद कर रहे हैं। – EpicPandaForce

+0

आपका क्या मतलब है? मेरे AppComponent में मैं अपने ऐप मॉड्यूल को इंगित करता हूं और IntentStarter – Tobias

+1

के लिए एक प्रदान विधि है, मैं वास्तव में डैगर 2 में नहीं हूं, लेकिन शायद यह मदद करता है? http://siphon9.net/loune/2015/04/dagger-2-0-android- माइग्रेशन-tips/ (अपनी त्रुटि खोजने के लिए नीचे देखें - अपने घटक के लिए 'प्रदान करेंन्टेंट स्टार्टर' विधि जोड़ने का प्रयास करें) उम्मीद है कि इससे मदद मिलती है । –

उत्तर

4

पहली बात यह है कि, जैसा कि मैंने कहा था, आपके प्रावधान विधियां आपके घटकों से गुम हैं। उदाहरण के लिए,

@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 
} 

और तुम क्षेत्र इंजेक्शन के साथ एक गलती कर रहे हैं, अपने IntentStarter या तो कॉल करने के लिए की जरूरत है appComponent.inject(this) या एक निर्माता पैरामीटर में अपने संदर्भ पाने की जरूरत है।

इसके अलावा, मुझे लगता है कि @Provides एनोटेटेड विधि को स्कोप्ड प्रदाता प्राप्त करने के लिए @Singleton स्कोप की आवश्यकता है, और मॉड्यूल को चिह्नित करने से वास्तव में कुछ भी नहीं होता है।

तो, विशिष्ट होना,

@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; 
    } 
} 
संबंधित मुद्दे