2016-07-26 97 views
1

我试图用匕首2我的项目中实施改造,这让我下面的错误:Android的匕首2是要求@Provides而它的存在

Error:(22, 10) error: com.toranj.tyke.restApi.LotteryApiInterface cannot be provided without an @Provides- or @Produces-annotated method. 
com.toranj.tyke.restApi.LotteryApiInterface is injected at 
com.toranj.tyke.ui.MainActivity.LotteryApiInterface 
com.toranj.tyke.ui.MainActivity is injected at 
com.toranj.tyke.dagger.components.NetworkComponent.inject(activity) 

所以这里是我的LotteryApiInterface

public interface LotteryApiInterface { 
    @GET("/lottery/search") 
    Call<List<Lottery>> getByCriteria(@Query("q") String query); 

    @GET("lottery/details") 
    Call<Lottery> getById(@Query("Id") String id); 
} 

这里是LotteryComponent

@PerActivity 
@Component(dependencies = NetworkComponent.class, modules = LotteryModule.class) 
public interface LotteryComponent { 
    void inject(MainActivity activity); 

} 

和还有我LotteryModule

@Module 
public class LotteryModule { 

    @Provides 
    @PerActivity 
    public LotteryApiInterface providesLotteryApiInterface(Retrofit retrofit) { 
     return retrofit.create(LotteryApiInterface.class); 
    } 
} 

当我清洁>重建它给了我,即使我LotteryModule拥有该对象的@Provides注释错误的项目。

也许我在这里错过了一些东西。任何帮助表示赞赏。

编辑

public class TykeApp extends Application { 

    private NetworkComponent networkComponent; 

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

     networkComponent = DaggerNetworkComponent.builder() 
       .appModule(new AppModule(this)) 
       .networkModule(new  NetworkModule("http://192.168.0.3:8080")) 
       .build(); 
    } 

    public NetworkComponent getNetworkComponent() { 
     return networkComponent; 
    } 
} 

NetworkComponent

@Singleton 
@Component(modules = { AppModule.class, NetworkModule.class }) 
public interface NetworkComponent { 
    void inject(MainActivity activity); 

    Retrofit retrofit(); 
    OkHttpClient okHttpClient(); 
    SharedPreferences sharedPreferences(); 
} 

NetworkModule:

@Module 
public class NetworkModule { 

    String baseUrl; 

    public NetworkModule(String baseUrl) { 
     this.baseUrl = baseUrl; 
    } 

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

    @Provides 
    OkHttpClient provideOkhttpClient() { 
     OkHttpClient.Builder client = new OkHttpClient.Builder(); 
     return client.build(); 
    } 

    @Provides 
    Retrofit provideRetrofit(OkHttpClient httpClient) { 
     return new Retrofit.Builder() 
       .addConverterFactory(GsonConverterFactory.create()) 
       .baseUrl(baseUrl) 
       .client(httpClient) 
       .build(); 
    } 
} 
改造在下面我的应用类中创建

,这是在MainActivity:

public class MainActivity extends BaseActivity { 

    @Inject 
    Retrofit retrofit; 

    @Inject 
    LotteryApiInterface LotteryApiInterface; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ((TykeApp)getApplication()).getNetworkComponent().inject(this); 

     LotteryComponent lotteryComponent = DaggerLotteryComponent.builder() 
       .lotteryModule(new LotteryModule()) 
       .build(); 
     lotteryComponent.inject(this); 
    } 
} 

当我使用

@Inejct 
LotteryApiInterface LotteryApiInterface; 

它抛出指定的错误消息。

+0

你在哪里创建'Retrofit'本身? – Divers

+0

改造是在应用程序中创建的,它是单例,它工作得很好。我会更新问题 –

+0

改装传递给该方法'公众LotteryApiInterface providesLotteryApiInterface(改造改造){'应该用匕首 – Divers

回答

2

当您在使用Dagger2领域,那么你的组件,您使用component.inject(this);与需要能够提供每个依赖你打上@Inject

这意味着,在你的情况,你不应该有

((TykeApp)getApplication()).getNetworkComponent().inject(this); 

,而是你应该有

LotteryComponent lotteryComponent = DaggerLotteryComponent.builder() 
      .networkComponent(((TykeApp)getApplication()).getNetworkComponent()) 
      .lotteryModule(new LotteryModule()) 
      .build(); 
    lotteryComponent.inject(this); 

但值得注意的是,您的网络模块应该是

@Module 
public class NetworkModule { 

    String baseUrl; 

    public NetworkModule(String baseUrl) { 
     this.baseUrl = baseUrl; 
    } 

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

    @Provides 
    @Singleton 
    OkHttpClient provideOkhttpClient() { 
     OkHttpClient.Builder client = new OkHttpClient.Builder(); 
     return client.build(); 
    } 

    @Provides 
    @Singleton 
    Retrofit provideRetrofit(OkHttpClient httpClient) { 
     return new Retrofit.Builder() 
       .addConverterFactory(GsonConverterFactory.create()) 
       .baseUrl(baseUrl) 
       .client(httpClient) 
       .build(); 
    } 
} 

而你的NetworkComponent应该是

@Singleton 
@Component(modules = { AppModule.class, NetworkModule.class }) 
public interface NetworkComponent { 
    Retrofit retrofit(); 
    OkHttpClient okHttpClient(); 
    SharedPreferences sharedPreferences(); 
} 
+0

woh ...真棒,它的工作。谢谢您的帮助。我明白了你的观点。干杯 –