Dagger-2 的空指针异常 (android)
Null pointer exception of Dagger-2 (android)
详情
UseContex 上的空指针异常 class 使用 UseContex 的 'printToast()' 方法时 class.UseContex class 扩展了 mainActivity.If 我在 MainActivity 中打印 toast 而不是它的在上下文对象上不包含空指针,但在 UseContex 中不包含与它显示空指针异常相同的东西。
应用组件
@Singleton @Component(modules = {AppModule.class})
public interface AppComponent {
void inject(DaggerApplication daggerApplication);
void inject(MainActivity mainActivity);
}
AppModule
@Module
public class AppModule {
private final DaggerApplication application;
public AppModule(DaggerApplication application) {
this.application = application;
}
@Singleton
@Provides
Context providesApplicationContext(){
return application;
}
@Singleton
@Provides
UseContex provideUsecontex(){
return new UseContex();
}
}
UseContex
public class UseContex extends MainActivity{
public void printToast(){
Log.e("User dao impl","Hello user dao");
Toast.makeText(context, "helo", Toast.LENGTH_SHORT).show();
}
}
主要活动
public class MainActivity extends AppCompatActivity {
@Inject
UseContex useContex;
@Inject
public Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((DaggerApplication)getApplication()).getAppComponent().inject(this);
useContex.printToast();
}
}
匕首应用程序
public class DaggerApplication extends Application {
AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
appComponent = DaggerAppComponent.builder().appModule(new
AppModule(this)).build();
appComponent.inject(this);
}
public AppComponent getAppComponent(){return appComponent;}
}
Dagger 没有注入你的 UseContex
subclass 因为 AppComponent
没有 @provide
UseContex
。 AppComponent
只是 @providing
一个 MainActivity
并且您正在传递一个 UseContex
作为它的多态基础 class 并希望它起作用。相反,AppComponent
中的 @provide
一个 UseContex
和 Dagger 将注入你的基础 class 字段。
它显示空指针,因为上下文未在 UseContext class 中定义。
您必须在这一行
中使用 "getApplicationContext" 代替 "context"
Toast.makeText(context, "helo", Toast.LENGTH_SHORT).show();
替换
Toast.makeText(getApplicationContext, "helo", Toast.LENGTH_SHORT).show();
详情
UseContex 上的空指针异常 class 使用 UseContex 的 'printToast()' 方法时 class.UseContex class 扩展了 mainActivity.If 我在 MainActivity 中打印 toast 而不是它的在上下文对象上不包含空指针,但在 UseContex 中不包含与它显示空指针异常相同的东西。
应用组件
@Singleton @Component(modules = {AppModule.class})
public interface AppComponent {
void inject(DaggerApplication daggerApplication);
void inject(MainActivity mainActivity);
}
AppModule
@Module
public class AppModule {
private final DaggerApplication application;
public AppModule(DaggerApplication application) {
this.application = application;
}
@Singleton
@Provides
Context providesApplicationContext(){
return application;
}
@Singleton
@Provides
UseContex provideUsecontex(){
return new UseContex();
}
}
UseContex
public class UseContex extends MainActivity{
public void printToast(){
Log.e("User dao impl","Hello user dao");
Toast.makeText(context, "helo", Toast.LENGTH_SHORT).show();
}
}
主要活动
public class MainActivity extends AppCompatActivity {
@Inject
UseContex useContex;
@Inject
public Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((DaggerApplication)getApplication()).getAppComponent().inject(this);
useContex.printToast();
}
}
匕首应用程序
public class DaggerApplication extends Application {
AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
appComponent = DaggerAppComponent.builder().appModule(new
AppModule(this)).build();
appComponent.inject(this);
}
public AppComponent getAppComponent(){return appComponent;}
}
Dagger 没有注入你的 UseContex
subclass 因为 AppComponent
没有 @provide
UseContex
。 AppComponent
只是 @providing
一个 MainActivity
并且您正在传递一个 UseContex
作为它的多态基础 class 并希望它起作用。相反,AppComponent
中的 @provide
一个 UseContex
和 Dagger 将注入你的基础 class 字段。
它显示空指针,因为上下文未在 UseContext class 中定义。 您必须在这一行
中使用 "getApplicationContext" 代替 "context"Toast.makeText(context, "helo", Toast.LENGTH_SHORT).show();
替换
Toast.makeText(getApplicationContext, "helo", Toast.LENGTH_SHORT).show();