我的应用程序中有一个网络组件,可以让我为我的活动注入改造功能。片段,我想把它注入我的Job课程,这就是我做的事情
NetComponent接口:
@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);
}
在我的Job Class中,我这样注入:
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;
}
}
答案 0 :(得分:1)
通过更改
来管理解决问题 void inject(Context cont);
到
void inject(LogUploader lp);
并在LogUploader中
((MyApp) this.getContext().getApplicationContext()).getNetComponent().inject(this);
我之前尝试过没有getApplicationContext,这是第一次崩溃,在它发生变化之后。
基本上注射需要让你想要注射的课程无论是活动片段还是其他任何内容都无关紧要。
答案 1 :(得分:0)
接受的答案对我没有用,因为在构造函数中尚未创建Job,您将收到错误。
您需要在任何其他代码之前将此代码段放在onRunJob()
方法中。
((MyApp) this.getContext().getApplicationContext()).getNetComponent().inject(this);