UncaughtException:java.lang.ExceptionInInitializerError

时间:2017-09-07 21:29:10

标签: android dagger-2

我在注入ServiceGenerator.class时收到以下错误。

我正在尝试从主要用于改造api服务生成的ServiceGenerator.Class注入SignUpActivity.Class,并依赖于我从Retrofit.Builder公开的构造函数注入注入的ApplicationComponent.Class实例。

SignUpActivity.Class

Public class SignUpActivity extends AppCompatActivity {

  @Inject
  ServiceGenerator serviceGenerator;

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

    AppGenerator.getPresenterComponent().inject(this);

  }

  private void saveMobileNumberService(String mNumber) {

        MobileNumberService mobileService =
                serviceGenerator.createService(MobileNumberService.class);

        ..

  }

}

ServiceGenerator.Class

public class ServiceGenerator {
  private static Retrofit.Builder builder;

  @Inject
  public ServiceGenerator(Retrofit.Builder builder) {
    this.builder = builder;

  }

  private static Retrofit retrofit = builder.build();

  public static <S> S createService(Class<S> serviceClass) {
    return retrofit.create(serviceClass);

  }

}

NetModule.class

@Module
public class NetModule {

    ....

    @Singleton
    @Provides
      ServiceGenerator provideServiceGenerator(Retrofit.Builder builder) {
      return new ServiceGenerator(builder);
    }

    @Singleton
    @Provides
      Retrofit.Builder provideRetrofitBuilder() {
      Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(baseUrl)
      return retrofitBuilder;
    }

}

ApplicationComponent.Class

@Singleton
@Component( modules = {AppModule.class, NetModule.class} )

public interface ApplicationComponent {

  ..

  Retrofit.Builder exposeRetrofit();
  ServiceGenerator exposeServiceGenerator();

}

PresentationComponent.Class

@PerActivity
@Component(modules = PresentationModule.class, dependencies = ApplicationComponent.class)

public interface PresenterComponent {
    void inject (SignUpActivity signUpActivity);
}

错误

我在运行时收到以下错误。

UncaughtException: java.lang.ExceptionInInitializerError
                                                             at com.project.android.dependencies.modules.NetModule.provideServiceGenerator(NetModule.java:80)
                                                                        at com.project.android.dependencies.modules.NetModule_ProvideServiceGeneratorFactory.get(NetModule_ProvideServiceGeneratorFactory.java:26)
                                                                        at com.project.android.dependencies.modules.NetModule_ProvideServiceGeneratorFactory.get(NetModule_ProvideServiceGeneratorFactory.java:10)
                                                                        at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
                                                                        at com.project.android.dependencies.components.DaggerApplicationComponent.exposeServiceGenerator(DaggerApplicationComponent.java:120)
                                                                        at com.project.android.dependencies.components.DaggerPresenterComponent$com_project_android_dependencies_components_ApplicationComponent_exposeServiceGenerator.get(DaggerPresenterComponent.java:327)
                                                                        at com.project.android.dependencies.components.DaggerPresenterComponent$com_project_android_dependencies_components_ApplicationComponent_exposeServiceGenerator.get(DaggerPresenterComponent.java:314)
                                                                        at com.project.android.presentation.frameworks.onboarding.SignUpActivity_MembersInjector.injectMembers(SignUpActivity_MembersInjector.java:26)
                                                                        at com.project.android.presentation.frameworks.onboarding.SignUpActivity_MembersInjector.injectMembers(SignUpActivity_MembersInjector.java:8)
                                                                        at com.project.android.dependencies.components.DaggerPresenterComponent.inject(DaggerPresenterComponent.java:162)
                                                                        at com.project.android.presentation.frameworks.onboarding.SignUpActivity.onCreate(SignUpActivity.java:79)
                                                                        at android.app.Activity.performCreate(Activity.java:6119)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2618)
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:183)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                        at android.os.Looper.loop(Looper.java:194)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5651)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'retrofit2.Retrofit retrofit2.Retrofit$Builder.build()' on a null object reference
                                                                        at com.project.android.presentation.entrypoints.ServiceGenerator.<clinit>(ServiceGenerator.java:47)
                                                                        at com.project.android.dependencies.modules.NetModule.provideServiceGenerator(NetModule.java:80) 
                                                                        at com.project.android.dependencies.modules.NetModule_ProvideServiceGeneratorFactory.get(NetModule_ProvideServiceGeneratorFactory.java:26) 
                                                                        at com.project.android.dependencies.modules.NetModule_ProvideServiceGeneratorFactory.get(NetModule_ProvideServiceGeneratorFactory.java:10) 
                                                                        at dagger.internal.DoubleCheck.get(DoubleCheck.java:47) 
                                                                        at com.project.android.dependencies.components.DaggerApplicationComponent.exposeServiceGenerator(DaggerApplicationComponent.java:120) 
                                                                        at com.project.android.dependencies.components.DaggerPresenterComponent$com_yangout_android_dependencies_components_ApplicationComponent_exposeServiceGenerator.get(DaggerPresenterComponent.java:327) 
                                                                        at com.project.android.dependencies.components.DaggerPresenterComponent$com_yangout_android_dependencies_components_ApplicationComponent_exposeServiceGenerator.get(DaggerPresenterComponent.java:314) 
                                                                        at com.project.android.presentation.frameworks.onboarding.SignUpActivity_MembersInjector.injectMembers(SignUpActivity_MembersInjector.java:26) 
                                                                        at com.project.android.presentation.frameworks.onboarding.SignUpActivity_MembersInjector.injectMembers(SignUpActivity_MembersInjector.java:8) 
                                                                        at com.project.android.dependencies.components.DaggerPresenterComponent.inject(DaggerPresenterComponent.java:162) 
                                                                        at com.project.android.presentation.frameworks.onboarding.SignUpActivity.onCreate(SignUpActivity.java:79) 
                                                                        at android.app.Activity.performCreate(Activity.java:6119) 
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112) 
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491) 
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2618) 
                                                                        at android.app.ActivityThread.access$800(ActivityThread.java:183) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                        at android.os.Looper.loop(Looper.java:194) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5651) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

我已从[{1}}公开ServiceGenerator.Class并使用ApplicationComponent.ClassPresentationComponent.Class注入。我正在努力弄清楚为什么我得到这个SignUpActivity.Class

3 个答案:

答案 0 :(得分:0)

您的ServiceGenerator存在问题(请参阅评论):

public class ServiceGenerator {
  private static Retrofit.Builder builder; //no!! use Dagger 2 @Singletons instead

  @Inject
  public ServiceGenerator(Retrofit.Builder builder) {
    this.builder = builder;

  }

  private static Retrofit retrofit = builder.build(); //no! don't do this

  public static <S> S createService(Class<S> serviceClass) {
    return retrofit.create(serviceClass);

  }
}

在Dagger 2通过构造函数提供builder.build()之前将调用Builder因此空指针异常。

Retrofit内维护ServiceGenerator的静态引用可能不是您想要的解决方案。依赖注入和滚动自己的静态单例缓存就像精灵和矮人一样 - 它们并不能很好地共存。

事实上,整个ServiceGenerator是您可能不需要的额外重定向层。

为什么不配置Dagger 2来提供Retrofit而不是Retrofit.Builder?然后,您将不需要使用静态字段自行管理Retrofit实例:

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

然后,如果你仍然需要ServiceGenerator,那就更简单了:

public class ServiceGenerator {
    private final Retrofit retrofit;

    @Inject
    public ServiceGenerator(Retrofit retrofit) {
        this.retrofit = retrofit;
    }

    public static <S> S createService(Class<S> serviceClass) {
        return retrofit.create(serviceClass);
    }
}

答案 1 :(得分:0)

更新改造 2.8.1。现在已经解决了。

答案 2 :(得分:-2)

更新改造到 2.9.0,实现 'com.squareup.retrofit2:retrofit:2.9.0'