java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object javax.inject.Provider.get()' on a null object reference
at biz.golek.whattodofordinner.MainActivity.onOptionsItemSelected(MainActivity.java:57)
这是:
settings.get().Run();我创建了:
MainActivity:
public class MainActivity extends AppCompatActivity {
@Inject
Provider<ShowSettingsController> settings;
/.../
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
settings.get().Run();
return true;
}
return super.onOptionsItemSelected(item);
}
}
应用:
public class WhatToDoForDinnerApp extends com.orm.SugarApp {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityInjector());
}
}
ActivityInjector:
public class ActivityInjector implements Application.ActivityLifecycleCallbacks {
private ApplicationComponent component;
public ActivityInjector(){
this.component = DaggerApplicationComponent.builder()
.settingsModule(new SettingsModule())
.build();
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
component.inject(activity);
}
/.../
}
应用组件:
@Singleton
@Component(modules = { SettingsModule.class })
public interface ApplicationComponent {
void inject(Activity activity);
}
并且是SettingsModule模块:
@Module
public class SettingsModule {
@Provides
@Singleton
static ShowSettingsController provideShowSettingsController(){
return new ShowSettingsControllerImpl();
}
/.../
}
整个代码在我的Github上,链接为:whattodofordinner。 我有什么错误的想法吗? 提前感谢您。