试图注入到工作

问题描述:

我有我的应用程序的网络组件,让我注入改装到我的活动&片段,我想把它注入到我的工作类,这里就是我所做的试图注入到工作

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); 
} 

在我的作业类我注入这样的:

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 

任何想法我应该做什么不同? 感谢所有的帮手!

UPDATE

第一崩溃(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

请为您添加翻新提供逻辑。 –

+0

增加了整个网络模块,谢谢 – yanivtwin

+0

请提供NPE的完整堆栈跟踪 – crgarridos

管理通过改变来解决它

void inject(Context cont); 

void inject(LogUploader lp); 

并在LogUploader到

 ((MyApp) this.getContext().getApplicationContext()).getNetComponent().inject(this); 

我之前尝试过,没有getApplicationContext,这是第一次崩溃,它改变后的工作。

基本上,注入需要获得想要注入的类,如果它是Activity Fragment或其他类型,则无关紧要。