Otto 和 Dagger 的 NullPointerException
NullPointerException with Otto and Dagger
我是 Otto 的新手,我在确定它与 Dagger 依赖注入器和 JobManager 一起运行时遇到了严重的问题。每当我启动我的应用程序时,我都会收到相同的错误消息:
"java.lang.RuntimeException: Unable to resume activity {sdos.juanjosemelero.pruebaormlite2/sdos.juanjosemelero.pruebaormlite2.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.squareup.otto.Bus.register(java.lang.Object)' on a null object reference"
[...]
"Caused by java.lang.NullPointerException:
Attempt to invoke virtual method 'void com.squareup.otto.Bus.register(java.lang.Object)' on a null object reference".
它引用的是这一行:
bus.register(this);
这是我的 activity:
public class MainActivity extends ActionBarActivity {
@Inject Bus bus;
@Override
protected void onResume() {
super.onResume();
bus.register(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
bus.unregister(this);
}
}
这是我的 Dagger 注入模块:
@Module(
injects = {MainActivity.class},
library = true
)
public class MyModule {
private final MyApplication application;
public MyModule (MyApplication application) {
this.application = application;
}
@Provides
@Singleton
MyApplication provideMyApplication() {
return application;
}
@Provides
@Singleton
public Bus provideBus (){
return new Bus(ThreadEnforcer.ANY);
}
}
还有MyApplicationclass如果有兴趣可以看看:
public class MyApplication extends Application {
private ObjectGraph objectGraph;
private static MyApplication application;
//Constructor
public MyApplication() {
application = this;
}
@Override
public void onCreate() {
super.onCreate();
buildObjectGraph();
}
public void buildObjectGraph() {
objectGraph = ObjectGraph.create(new MyModule(this));
}
public void inject(Object o) {
objectGraph.inject(o);
}
public static MyApplication get() {
return application;
}
}
我应该以某种方式初始化 MainActivity 吗?
您没有使用对象图在 onCreate 中注入 MainActivity。就这样。
直接打电话
MyApplication.get().inject(this);
在 MainActivity onCreate() 中。
我是 Otto 的新手,我在确定它与 Dagger 依赖注入器和 JobManager 一起运行时遇到了严重的问题。每当我启动我的应用程序时,我都会收到相同的错误消息:
"java.lang.RuntimeException: Unable to resume activity {sdos.juanjosemelero.pruebaormlite2/sdos.juanjosemelero.pruebaormlite2.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.squareup.otto.Bus.register(java.lang.Object)' on a null object reference"
[...]
"Caused by java.lang.NullPointerException:
Attempt to invoke virtual method 'void com.squareup.otto.Bus.register(java.lang.Object)' on a null object reference".
它引用的是这一行:
bus.register(this);
这是我的 activity:
public class MainActivity extends ActionBarActivity {
@Inject Bus bus;
@Override
protected void onResume() {
super.onResume();
bus.register(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
bus.unregister(this);
}
}
这是我的 Dagger 注入模块:
@Module(
injects = {MainActivity.class},
library = true
)
public class MyModule {
private final MyApplication application;
public MyModule (MyApplication application) {
this.application = application;
}
@Provides
@Singleton
MyApplication provideMyApplication() {
return application;
}
@Provides
@Singleton
public Bus provideBus (){
return new Bus(ThreadEnforcer.ANY);
}
}
还有MyApplicationclass如果有兴趣可以看看:
public class MyApplication extends Application {
private ObjectGraph objectGraph;
private static MyApplication application;
//Constructor
public MyApplication() {
application = this;
}
@Override
public void onCreate() {
super.onCreate();
buildObjectGraph();
}
public void buildObjectGraph() {
objectGraph = ObjectGraph.create(new MyModule(this));
}
public void inject(Object o) {
objectGraph.inject(o);
}
public static MyApplication get() {
return application;
}
}
我应该以某种方式初始化 MainActivity 吗?
您没有使用对象图在 onCreate 中注入 MainActivity。就这样。
直接打电话
MyApplication.get().inject(this);
在 MainActivity onCreate() 中。