I am using material dialog date picker and material dialog core
I am facing a major problem. I dont know why?
private fun showDatePicker() {
MaterialDialog(this).show {
datePicker(requireFutureDate = false) { _, dateTime ->
// Use dateTime (Calendar)
val date =
"${dateTime.get(Calendar.YEAR)}-${dateTime.get(Calendar.MONTH)}-${dateTime.get(Calendar.DATE)}"
tvDob.text = date
presenter.setDob(date)
}
}
}
In the above code, I am launching date picker and when I am click on ok button. it says tvDob is null. But why?
I noticed it happens for every thing. I mean. material dialog core, list, input anything.
What am I doing wrong?
Well, where is tvDob defined? It doesn't appear to be in your dialog so this is not related to this library.
@afollestad But hey bro. If I create my own custom dialog and provide a callback to set the data that I selected from my dialog list or dialog view then its work fine.
But if I create custom dialog using your library this view throw exception about null.
I think it is library problem.....
tvDob is a textview id in an activity from where I tried to show to dialog
So tvDob is pointing to a view in your Activity? That's not related to the Dialog
If you're acting on a custom view in the Dialog you need to use dialog.getCustomView()
@afollestad
I think I failed to understand you.
tvDob is a text view in my activity from which I start the dialog.
Yes tvDob view is not related to dialog. But in a way it is related.
datePicker(requireFutureDate = false) { _, dateTime ->
// Use dateTime (Calendar)
val date =
"${dateTime.get(Calendar.YEAR)}-${dateTime.get(Calendar.MONTH)}-${dateTime.get(Calendar.DATE)}"
tvDob.text = date
}
See the above callback. it returns the data on mainthread (UI thread) right? But tvDob returns null. view is null.
But I created the dialog from this same activity. It must not be null.
If I use my own (custom) dialog then my callback working fine.. view is not null and data is showing on the view.
But if I use material dialog. callback returns data but view is null.
My custom dialog
D.createListDialog(this, items, object : SimpleCallback2<String, Int> {
override fun callback(obj1: String, obj2: Int) {
tvDob .text = obj1
}
})
My main talk is in brief
tvDob is the view of my activity not a custom dialog view
If I start or show material dialog from this activity and its callback returns the data my view should not be null.
There was no screen rotation.
For some other dialog it works just fine.
Just so you know, you format multiple lines of code with 3 ticks above and 3 ticks below :)
But the dialog doesn't touch your Activity so there's no way it could be nullifying anything. It's just an android.app.Dialog.
I had a similar issue with a custom dialog. My solution was to create another variable using findViewById within the call back in order to update the view on the activity.
I think your issue is, that you're in the wrong scope...
When using the android kotlin extensions, it's import to ensure you're calling from the right scope, means the component holding the layout (activity, fragment,...)
Try to replace the tvDob by this@Activity/Fragment.tvDob.
@guger Thanks. You suggestion solved it
@guger Thanks! Thanks! Thanks!
Most helpful comment
I think your issue is, that you're in the wrong scope...
When using the android kotlin extensions, it's import to ensure you're calling from the right scope, means the component holding the layout (activity, fragment,...)
Try to replace the
tvDobbythis@Activity/Fragment.tvDob.