Answer the question
In order to leave comments, you need to log in
Why, when I use the kotlin-android-extension to access the view by its name, does it return a null value?
Using kotlin-android-extensions, I called the view by name and tried to use its method, but I got a nullPointerException. Although ide itself told me that there is such a specific myCalendar view in such and such an xml file. Also, I want to note, I tried to give the variable a link in the standard way through findViewById, then again use the view method, in which case everything worked.
ps you probably ask: "why didn't I catch null before?", because even before that I tried to access myCalendar, then I will immediately answer you: my array was zero size, the cycle did not start.
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.applandeo.materialcalendarview.EventDay
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.QueryDocumentSnapshot
import kotlinx.android.synthetic.main.fragment_timetable.*
import java.util.*
import kotlin.collections.ArrayList
import com.example.simbirsoftproject.Data as MyData
class MyEventDay(day: Calendar, imageResource: Int, var data: MyData) : EventDay(day, imageResource)
class FragmentTimetable : Fragment() {
private var events: MutableList<EventDay> = ArrayList()
val db: FirebaseFirestore = FirebaseFirestore.getInstance()
private val dataOfLessons: MutableList<MyData> = getData()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val view: View = inflater.inflate(R.layout.fragment_timetable, container, false)
for (data: MyData in dataOfLessons) {
Toast.makeText(
context,
data.getDay() + "." + data.getMonth() + "." + data.getYear(),
Toast.LENGTH_SHORT
).show()
val calendar: Calendar = Calendar.getInstance()
calendar.set(data.getDay() as Int, data.getMonth() as Int, data.getYear() as Int)
myCalendar.setDate(calendar)
events.add(EventDay(calendar, R.drawable.fui_ic_googleg_color_24dp))
}
myCalendar.setEvents(events) //здесь я ловлю маслину (null)
return view
}
fun getData(): MutableList<com.example.simbirsoftproject.Data> {
val dataOfLessons: MutableList<MyData> = ArrayList()
db.collection("GroupLessons")
.get()
.addOnCompleteListener { p0 ->
for (document: QueryDocumentSnapshot in p0.result!!) {
Log.d("logmy", "В цикле1")
val dataClass: MyData = document.toObject(MyData::class.java)
Log.d("logmy", "В цикле2")
dataOfLessons.add(
MyData(
dataClass.getName(),
dataClass.getDescription(),
dataClass.getPhotoID(),
dataClass.getStartTime(),
dataClass.getEndTime(),
dataClass.getDay(),
dataClass.getMonth(),
dataClass.getYear()
)
)
Log.d("logmy", "В цикле3")
}
}
return dataOfLessons
}
}
Answer the question
In order to leave comments, you need to log in
Well, in general, to use magic, you need to know how it works.
Read this , for example.
These properties are synthetic, they don't really exist. The compiler replaces them with access to the cache views. And he should fill them with the help of findViewById. But you haven't returned the view from the onCreateView method yet, no one can guess where to call find.
Summary. Use synthetic properties in the onViewCreated method. Better not use it at all, well, what the hell, this magic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question