I have a problem trying to change a TextView through a callback using an object provided by Dagger2. If I make the instance of the same object of the common form (new Object ..) everything works. But if it is done through the dagger instance the TextView NullPointerException appears. I think the problem is in the module in the way that the MainActivity context happened but I'm not sure.
The Activity
class MainActivity : AppCompatActivity(), MyCallBack{
@Inject
lateinit var myObject: MyObject
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mytextview.text = "first change"
val myComponent = (application as App).myComponent
myComponent.inject(this)
}
override fun callBack(string: String) {
try {
mytextview.text = string
} catch (e: Exception) {
Log.d("xxx", e.toString())
}
}
}
Objecto with the callback
class MyObject(var res: MyCallBack) {
init {
res.callBack("second change")
}
}
Dagger component
@Component(modules = MyModule.class)
public interface MyComponent {
void inject(MainActivity mainActivity);
}
Dagger module
@Module
class MyModule {
@Provides
fun proviesMyObject(): MyObject = MyObject(MainActivity())
}