質問編集履歴

2

DatePickerFragment追加

2020/02/04 23:44

投稿

torepyon
torepyon

スコア4

test CHANGED
File without changes
test CHANGED
@@ -256,6 +256,72 @@
256
256
 
257
257
 
258
258
 
259
+ Dialog
260
+
261
+ --
262
+
263
+ class DatePickerFragment : DialogFragment(),
264
+
265
+ DatePickerDialog.OnDateSetListener {
266
+
267
+
268
+
269
+ interface OnDateSelectedListener {
270
+
271
+ fun onSelected(year: Int, month: Int, date: Int)
272
+
273
+ }
274
+
275
+
276
+
277
+ private lateinit var listener: OnDateSelectedListener
278
+
279
+
280
+
281
+ override fun onAttach(context: Context?) {
282
+
283
+ super.onAttach(context)
284
+
285
+ if(context is OnDateSelectedListener) {
286
+
287
+ listener = context
288
+
289
+ }
290
+
291
+ }
292
+
293
+
294
+
295
+ override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
296
+
297
+ val c = Calendar.getInstance()
298
+
299
+ val year = c.get(Calendar.YEAR)
300
+
301
+ val month = c.get(Calendar.MONTH)
302
+
303
+ val date = c.get(Calendar.DAY_OF_MONTH)
304
+
305
+ return DatePickerDialog(context, this, year, month, date)
306
+
307
+ }
308
+
309
+
310
+
311
+ override fun onDateSet(view: DatePicker, year:
312
+
313
+ Int, month: Int, date: Int) {
314
+
315
+ ```listener.onSelected(year, month, date)
316
+
317
+ ```
318
+
319
+ }
320
+
321
+ }
322
+
323
+
324
+
259
325
  試したこと
260
326
 
261
327
  --

1

試したことを追記

2020/02/04 23:44

投稿

torepyon
torepyon

スコア4

test CHANGED
File without changes
test CHANGED
@@ -253,3 +253,13 @@
253
253
  }
254
254
 
255
255
  }
256
+
257
+
258
+
259
+ 試したこと
260
+
261
+ --
262
+
263
+ Dialogファイルを作成し、DatePickerFragment.OnDateSelectedListenerをMainActivityに書いたところ日付選択ダイアログは表示されたのですが、日付を選択した後落ちてしまいます。エラー内容(kotlin.UninitializedPropertyAccessException: lateinit property listener has not been initialized
264
+
265
+ at com.example.myalarmclock.DatePickerFragment.onDateSet)