Android 试图注入到工作类中
我的应用程序中有一个网络组件,允许我将改进注入到我的活动和片段中,我想将它注入到我的工作类中,下面是我所做的 NetComponent接口:Android 试图注入到工作类中,android,rx-java,retrofit2,dagger-2,Android,Rx Java,Retrofit2,Dagger 2,我的应用程序中有一个网络组件,允许我将改进注入到我的活动和片段中,我想将它注入到我的工作类中,下面是我所做的 NetComponent接口: @Singleton @Component(modules={AppModule.class, NetModule.class}) public interface NetComponent { void inject(MainActivity activity); void inject(SplashActivity activity);
@Singleton
@Component(modules={AppModule.class, NetModule.class})
public interface NetComponent {
void inject(MainActivity activity);
void inject(SplashActivity activity);
void inject(RegisterActivity activity);
void inject(SettingsFragment fragment);
void inject(Context cont); // also tried void inject(Job job);
}
在我的职业课上,我是这样注射的:
public class LogUploader extends Job {
public static final String TAG = "UPLOAD_LOGS" ;
@Inject
Retrofit mRetrofitClient;
@Override
@NonNull
protected Result onRunJob(Params params) {
((MyApp) getContext()).getNetComponent().inject(getContext());
// run your job here
Log.e("LogFile", " "+ TAG);
//// TODO: 10/18/2017 send log
checklogs(this.getContext());
//// TODO: 10/18/2017 get phone db update
return Result.SUCCESS;
}
}
而坠机事件:
ClassCastException: com.evernote.android.job.v21.PlatformJobService cannot be cast to com.**.**.Application.MyApp
有什么想法吗?我应该怎么做?
感谢所有的帮助者
更新
第一次崩溃CCE是因为我做了getContext并将其转换为MyApp,我将其更改为
((MyApp) this.getContext().getApplicationContext()).getNetComponent().inject(getContext());
现在,这次崩溃更有意义了:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object retrofit2.Retrofit.create(java.lang.Class)' on a null object reference
我检查了调试,注入行没有注入mRetrofitClient
有什么想法吗
NetModule类:
@Module
public class NetModule {
String mBaseUrl;
// Constructor needs one parameter to instantiate.
public NetModule(String baseUrl) {
this.mBaseUrl = baseUrl;
}
// Dagger will only look for methods annotated with @Provides
@Provides
@Singleton
// Application reference must come from AppModule.class
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
return gsonBuilder.create();
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient client = new OkHttpClient().newBuilder().cache(cache).build();
return client;
}
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
return retrofit;
}
}
设法通过改变
void inject(Context cont);
到
并在日志上传器中
((MyApp) this.getContext().getApplicationContext()).getNetComponent().inject(this);
我以前在没有getApplicationContext的情况下尝试过它,这是它工作后的第一次崩溃
基本上,注入需要获得您想要注入的类,不管它是活动片段还是其他任何类型。接受的答案对我不起作用,因为尚未在构造函数中创建作业,您将得到一个错误 您需要将此代码段放在onRunJob方法中的任何其他代码之前
((MyApp) this.getContext().getApplicationContext()).getNetComponent().inject(this);
请添加您的改装提供逻辑。添加了整个网络模块,谢谢请提供NPE的完整堆栈跟踪
((MyApp) this.getContext().getApplicationContext()).getNetComponent().inject(this);