没有@Provides或@Produces注释的方法无法提供-Android

时间:2018-07-06 08:25:46

标签: android rx-java mvp

在我的应用中,出现此错误。这是我的代码。

ApiModuleForName

@Module
public class ApiModuleForName {

public final String BASE_URL = "http://api.themoviedb.org/3/movie/";
public final String API_KEY = "8551c026bbf22a4a386ebb5b87a5296b";

@Provides
public OkHttpClient provideClient(){

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);

    return new OkHttpClient.Builder().addInterceptor(interceptor).addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {

            Request request = chain.request();

            HttpUrl url = request.url().newBuilder().addQueryParameter(
                    "api_key",
                    API_KEY
            ).build();

            request = request.newBuilder().url(url).build();

            return chain.proceed(request);
        }
    }).build();


}

@Provides
public Retrofit provideRetrofit(String baseURL, OkHttpClient client){
    return new Retrofit.Builder()
            .baseUrl(baseURL)
            .client(client)
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
 }

}

ApiModuleInfo

@Module
public class ApiModuleInfo {

public final String BASE_URL = "http://www.omdbapi.com";
public final String API_KEY = "4f1016c2";

@Provides
public OkHttpClient provideClient(){
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    return new OkHttpClient.Builder().addInterceptor(interceptor).addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            HttpUrl url = request.url().newBuilder().addQueryParameter(
                    "apiKey",
                    API_KEY
            ).build();
            request = request.newBuilder().url(url).build();
            return chain.proceed(request);
        }
    }).build();
}

@Provides
public Retrofit provideretrofit(String baseURL, OkHttpClient client){
    return new Retrofit.Builder()
            .baseUrl(baseURL)
            .client(client)
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
}

@Provides
public MoreInfoApiService provideApiService(){
    return provideretrofit(BASE_URL, provideClient()).create(MoreInfoApiService.class);
}

} 

ApplicationModule

@Module
public class ApplicationModule {

private Application application;

public ApplicationModule(Application application){
    this.application = application;
}

@Provides
@Singleton
public Context provideContext(){
    return application;
  }
}

ApplicationComponent

Singleton
@Component(modules = {ApplicationModule.class, ApiModuleForName.class, 
ApiModuleInfo.class})
public interface ApplicationComponent {

  void inject(MainActivity target);

}

应用

public class App extends Application{
private ApplicationComponent component;

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

    component = DaggerApplicationComponent.builder()
            .applicationModule(new ApplicationModule(this))
            .apiModuleForName(new ApiModuleForName())
            .apiModuleForInfo(new ApiModuleInfo())
            .build();
}

public ApplicationComponent getComponent() {

    return component;

 }

}

TopMoviesActivityMVP

public interface TopMoviesActivityMVP {

interface View{

    void updateData(MoviesModel moviesModel);

    void showSnackbar(String s);

}

interface Presenter{
    void loadData();

    void rxUnsubscribe();

    void setView(TopMoviesActivityMVP.View view);

}

interface Model{

    Observable<MoviesModel> result();

}
}

MainActivity

public class MainActivity extends AppCompatActivity implements TopMoviesActivityMVP.View{

private final String TAG = MainActivity.class.getSimpleName();

@BindView(R.id.recycler_view)
RecyclerView recyclerView;

@BindView(R.id.listActivity_rootView)
ViewGroup rootView;

private ListAdapter listAdapter;
private List<MoviesModel> resultList = new ArrayList<>();

@Inject
TopMoviesActivityMVP.Presenter presenter;

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

    ((App) getApplication()).getComponent().inject(this);

    ButterKnife.bind(this);

    listAdapter = new ListAdapter(resultList);
    recyclerView.setAdapter(listAdapter);
    recyclerView.addItemDecoration(new DividerItemDecoration(this));

    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}


@Override
protected void onResume() {
    super.onResume();
    presenter.setView(this);
    presenter.loadData();
}

@Override
protected void onStop() {
    super.onStop();
    presenter.rxUnsubscribe();
    resultList.clear();
    listAdapter.notifyDataSetChanged();
}

@Override
public void updateData(MoviesModel moviesModel) {
    resultList.add(moviesModel);
    if(resultList.isEmpty()){
        listAdapter.notifyItemInserted(0);
    }else {
        listAdapter.notifyItemInserted(resultList.size() - 1);
    }
    Log.d(TAG,"updateData: " + resultList.size());
}

@Override
public void showSnackbar(String s) {
    Snackbar.make(rootView, s, Snackbar.LENGTH_SHORT).show();
}
}

然后我明白了

error: theo.tziomakas.demoapp.topmovies.TopMoviesActivityMVP.Presenter 
cannot be provided without an @Provides- or @Produces-annotated method.
theo.tziomakas.demoapp.topmovies.TopMoviesActivityMVP.Presenter is 
injected at
theo.tziomakas.demoapp.topmovies.MainActivity.presenter
theo.tziomakas.demoapp.topmovies.MainActivity is injected at
theo.tziomakas.demoapp.topmovies.root.ApplicationComponent.
inject(target)

0 个答案:

没有答案