我正在尝试将我的Android上下文从一个模块注入到另一个模块。这是我目前的代码:
UserProfileModule.java
每当它尝试获取用户配置文件时,都会失败并显示对象为空。
编辑:
PizzaApplication.java
UserProfileModule.java
@Module(
library = true
)
public class UserProfileModule {
@Inject Context _context;
@Provides
public AccountUtils.UserProfile provideUserProfile() {
return AccountUtils.getUserProfile(_context);
}
}
RootModule.java
@Module(
injects = {
PizzaApplication.class,
UserProfileModule.class,
MainActivity.class
},
includes = {
UserProfileModule.class
},
library = true
)
public class RootModule {
private final Context _context;
public RootModule(Context context) {
_context = context;
}
@Provides
@Singleton
public Context provideApplicationContext() {
return _context;
}
}
每当它尝试获取用户配置文件时,都会失败并显示对象为空。
编辑:
PizzaApplication.java
public class PizzaApplication extends Application {
private ObjectGraph objectGraph;
@Override
public void onCreate() {
super.onCreate();
injectDependencies();
}
private void injectDependencies() {
objectGraph = ObjectGraph.create(new RootModule(this));
objectGraph.inject(this);
}
public void inject(Object object) {
objectGraph.inject(object);
}
}
MainActivity.java
public class MainActivity extends BaseActivity {
@InjectView(R.id.toolbar) public Toolbar _toolbar;
@InjectView(R.id.drawer) public DrawerFrameLayout _drawer;
@Inject public AccountUtils.UserProfile _profile;
@Inject public Context _context;
// private NavigationDrawerFragment navigationDrawerFragment;
@Override
protected void onCreate(Bundle saveInstanceState) {