• 欢迎访问,如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Dagger2 Caused by: java.lang.ClassCastException: androi

移动开发 码拜 3年前 (2017-04-27) 262次浏览
本人看过很多的博客资料什么的,获取传入Activity的实例,接收时context.getApplicationContext();但是为什么本人的就会强转失败呢

    public static   APPApplication getsInstance(Context context) {
        return (APPApplication)context.getApplicationContext();
    }

下面这个是本人的AppApplication.class

public class APPApplication extends Application {
    private AppComponent appComponent;
    public static   APPApplication getsInstance(Context context) {
        return (APPApplication)context.getApplicationContext();
    }
    @Override
    public void onCreate() {
        super.onCreate();
        setupComponent();
    }
    private void setupComponent() {
        appComponent=DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .gitHubApiModule(new GitHubApiModule())
                .build();
    }
    public AppComponent getAppComponent() {
        return appComponent;
    }
}

就是这里传入当前BaseActivity,到AppApplication,从而AppComponent实例

public abstract class BaseActvity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        setupAppComponent(APPApplication.getsInstance(this).getAppComponent());
    }
    protected abstract void setupAppComponent(AppComponent appComponent);
    protected abstract int getLayoutId();
}

MainActivity.class

public class MainActivity extends BaseActvity {
    @Inject
    GitHubApiService gitHubApiService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Call<List<Contributor>> listCall = gitHubApiService.contributorsBySimpleGetCall("square", "retrofit");
        listCall.enqueue(new Callback<List<Contributor>>() {
            @Override
            public void onResponse(Call<List<Contributor>> call, Response<List<Contributor>> response) {
                for (Contributor contributor:response.body()){
                    Log.i("contributor",contributor.getLogin());
                }
            }
            @Override
            public void onFailure(Call<List<Contributor>> call, Throwable t) {
            }
        });
    }
    @Override
    protected void setupAppComponent(AppComponent appComponent) {
        appComponent.inject(this);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
}
@Component(modules = {AppModule.class,GitHubApiModule.class})
public interface AppComponent {
    void inject(MainActivity activity);
}
@Module
public class AppModule {
    APPApplication appApplication;
    public AppModule(APPApplication appApplication) {
        this.appApplication = appApplication;
    }
    @Provides
    public APPApplication getAppApplication() {
        return appApplication;
    }
}
@Module
public class GitHubApiModule {
    @Provides
    public OkHttpClient provideOkHttpClient(){
        OkHttpClient okHttpClient=new OkHttpClient.Builder()
                .connectTimeout(60*1000,TimeUnit.MILLISECONDS)//设置连接超时时间
                .readTimeout(60*1000,TimeUnit.MICROSECONDS)//设置读取超时时间
                .build();
        return okHttpClient;
    }
    @Provides
    public Retrofit getRetrofit(APPApplication appApplication, OkHttpClient okHttpClient){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(appApplication.getString(R.string.base_url))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();
        return retrofit;
    }
    @Provides
    public GitHubApiService provideGitHubApiService(Retrofit retrofit){
        return retrofit.create(GitHubApiService.class);
    }
}
解决方案

20

是不是你没有在manifests 里面使用这个APPApplication

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Dagger2 Caused by: java.lang.ClassCastException: androi
喜欢 (0)
[1034331897@qq.com]
分享 (0)