Android:Dagger 2构造函数注入不会调用构造函数并最终在NPE中

问题描述 投票:1回答:1

好吧,我浏览了所有SO帖子以及在线教程和博客。我似乎无法理解我的dagger 2构造函数注入中的nullpointer异常背后的原因。

问题是构造函数没有被调用而是调用,

public void getMobileDataUsage(OnDatastoreResponse onDatastoreResponse)

并导致nullpointer

我有一个使用构造函数注入的单例APIClient类。

@Singleton
public class APIClient {

private static final String TAG = "APIClient";

private APIInterface apiInterface;
private Retrofit retrofit;

@Inject
public APIClient(Context context) {
    // use 10MB cache
    long cacheSize = 10 * 1024 * 1024;
    Cache cache = new Cache(context.getCacheDir(), cacheSize);

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).cache(cache).build();

    retrofit = new Retrofit.Builder()
            .baseUrl(BuildConfig.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(client)
            .build();

    this.apiInterface = retrofit.create(APIInterface.class);
}

public void getMobileDataUsage(OnDatastoreResponse onDatastoreResponse) {
    String resourceId = "a807b7ab-6cad-4aa6-87d0-e283a7353a0f";
    Integer limit = null;

    Single<DatastoreResponse> datastoreResponse = apiInterface.getMobileDataUsage(resourceId, limit);
    datastoreResponse.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new DisposableSingleObserver<DatastoreResponse>() {
                @Override
                public void onSuccess(DatastoreResponse datastoreResponse) {
                    if (datastoreResponse.getSuccess()) {
                        Log.d(TAG, "onSuccess: " + datastoreResponse.getSuccess());

                        onDatastoreResponse.onSuccessDatastoreResponse(datastoreResponse);

                    } else {
                        Log.e(TAG, "onSuccess: " + datastoreResponse.getSuccess());
                        onDatastoreResponse.onErrorResponse(new Exception("Datastore response not successful"));
                    }
                }

                @Override
                public void onError(Throwable e) {
                    Log.e(TAG, "onError: " + e.getMessage(), e);
                    onDatastoreResponse.onErrorResponse(e);
                }
            });
}

}

我有一个提供程序,为上面的构造函数注入提供Context。

@Module
public class ApplicationContextModule {

private final Context context;

public ApplicationContextModule(Context context) {
    this.context = context;
}

@Provides
Context provideApplicationContext() {
    return context;
}

}

以下是我的ApplicationComponent,

@Singleton
@Component(modules = {ApplicationContextModule.class, DataModule.class})
public interface ApplicationComponent {

void inject(MobileDataUsageActivity mobileDataUsageActivity);

APIClient apiClient();

Context context();

}

我的Application类构建组件,

public class MyApplication extends Application {

private ApplicationComponent applicationComponent;

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

    applicationComponent = DaggerApplicationComponent
            .builder()
            .applicationContextModule(new ApplicationContextModule(this))
            .dataModule(new DataModule())
            .build();

}

public ApplicationComponent getApplicationComponent() {
    return applicationComponent;
}
}

我在活动的onCreate期间注入实例,

((MyApplication) getApplication()).getApplicationComponent().inject(this);

最后我的存储库类抛出nullpointer异常。注意我有@Inject APIClient。但是在调试之后我注意到APIClient是null,因为它没有调用构造函数。

public class MobileDataRepository {

private static final String TAG = "MobileDataRepository";

@Inject
APIClient apiClient;

private List<Quarter> quarterList = new ArrayList<>();
private List<Year> yearList = new ArrayList<>();

private MutableLiveData<List<Year>> mutableYearList = new MutableLiveData<>();

public LiveData<List<Year>> getYearlyMobileDataUsage() {
    apiClient.getMobileDataUsage(new OnDatastoreResponse() {
        @Override
        public void onSuccessDatastoreResponse(DatastoreResponse datastoreResponse) {

            for (QuarterResponse q : datastoreResponse.getResult().getRecords()) {
                Log.d(TAG, "Quarter: " + q.get_id() + " : " + q.getQuarter());

                String quarterInfo[] = q.getQuarter().split("-");
                String year = quarterInfo[0];
                String quarterName = quarterInfo[1];

                quarterList.add(new Quarter(q.get_id(), q.getVolume_of_mobile_data(), Integer.parseInt(year), quarterName));
            }
            mutableYearList.setValue(yearList);
        }

        @Override
        public void onErrorResponse(Throwable e) {

        }
    });

    return mutableYearList;

}

}

并且没有创建告诉APIClient实例的异常(注意:我已经调试到已验证的APIClient为null),

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.channa.mobiledatausageapp/com.channa.mobiledatausageapp.view.MobileDataUsageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.channa.mobiledatausageapp.network.APIClient.getMobileDataUsage(com.channa.mobiledatausageapp.network.action.OnDatastoreResponse)' on a null object reference

对不起庞大的代码。我只想指出我已经完成了所有必需的工作,但由于一些奇怪的原因,构造函数DI不起作用。我甚至尝试使用@Provider for APIClient仍然是错误相同。提前致谢!

哦,我正在使用匕首版本:2.15

// Dagger 2
implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
java android dagger-2 dagger
1个回答
1
投票

要进行此注入,必须在ApplicationComponent中创建方法void inject(MobileDataRepository mobileRepository),然后在MobileDataRepository中获取对此组件的引用,并在某处调用此注入方法(例如,在构造函数中)

或者,为了使其更好,因为您已经有一个注入方法将依赖项注入MobileDataUsageActivity,您只需在MobileDataRepository中创建一个@Inject-annotated构造函数,然后将其注入您的活动。它看起来像这样:

class MobileDataRepository { 
    @Inject
    public MobileDataRepository(APIClient apiClient) 
    { //do your initialization } 
}

然后,在你的活动中:

class MobileDataUsageActivity {

    @Inject MobileDataRepository mobileDataRepository

// other code 
}

附:抱歉格式化,我用手机写了:)

© www.soinside.com 2019 - 2024. All rights reserved.