質問編集履歴

3

ソースコード修正

2022/06/09 07:50

投稿

bluvenz
bluvenz

スコア22

test CHANGED
File without changes
test CHANGED
@@ -74,6 +74,8 @@
74
74
  ```
75
75
  ```child1
76
76
  fun child1() {
77
+ // こちらの例外はparentでcatchされる
78
+ // child2のthrowはコメントアウト
77
79
  throw IOException("child1")
78
80
  }
79
81
  ```
@@ -81,6 +83,8 @@
81
83
  fun child2() {
82
84
  val coroutineScope = CoroutineScope(Job() + Dispatchers.IO)
83
85
  coroutineScope.launch {
86
+ // こちらの例外がparentでcatchされず、アプリが終了してしまう
87
+ // child1のthrowはコメントアウト
84
88
  throw IOException("child2")
85
89
  }
86
90
  }

2

ソースコード更新

2022/06/09 07:39

投稿

bluvenz
bluvenz

スコア22

test CHANGED
File without changes
test CHANGED
@@ -58,7 +58,7 @@
58
58
  // プログレスバー非表示
59
59
  binding.progressBar.visibility = View.INVISIBLE
60
60
 
61
- // 失敗メッセージ表示
61
+ // メッセージ表示
62
62
  val message =
63
63
  if (exceptionMessage == "") resources.getString(R.string.message_success)
64
64
  else resources.getString(R.string.message_failure) + "\n" + exceptionMessage

1

ソースコードを更新しました。

2022/06/09 07:37

投稿

bluvenz
bluvenz

スコア22

test CHANGED
File without changes
test CHANGED
@@ -16,7 +16,30 @@
16
16
  ### 該当のソースコード
17
17
 
18
18
  ```parent
19
+ override fun onNavigationItemSelected(item: MenuItem): Boolean {
20
+ when(item.itemId) {
21
+ // IO
22
+ R.id.nav_parent -> {
23
+ parent()
24
+ }
25
+ }
26
+
27
+ // 選択状態を解除
28
+ item.isCheckable = false
29
+ // ドロワーを閉じる
30
+ drawerLayout.closeDrawers()
31
+
32
+ return true
33
+ }
34
+
19
35
  private fun parent() {
36
+ // プログレスバー表示
37
+ binding.progressBar.visibility = View.VISIBLE
38
+
39
+ val handler = Handler(Looper.getMainLooper())
40
+
41
+ var exceptionMessage: String? = ""
42
+
20
43
  val coroutineScope = CoroutineScope(Job() + Dispatchers.IO)
21
44
  coroutineScope.launch {
22
45
  try {
@@ -29,9 +52,22 @@
29
52
  deferred.await()
30
53
 
31
54
  } catch(e: Exception) {
32
- (例外処理)
55
+ exceptionMessage = e.message
33
56
  } finally {
57
+ handler.post {
58
+ // プログレスバー非表示
59
+ binding.progressBar.visibility = View.INVISIBLE
60
+
61
+ // 失敗メッセージ表示
62
+ val message =
63
+ if (exceptionMessage == "") resources.getString(R.string.message_success)
64
+ else resources.getString(R.string.message_failure) + "\n" + exceptionMessage
65
+
66
+ Toast.makeText(
67
+ this@MainActivity,
34
- (終了処理)
68
+ message,
69
+ Toast.LENGTH_LONG
70
+ ).show()
35
71
  }
36
72
  }
37
73
  }
@@ -62,3 +98,4 @@
62
98
  Android開発について学習中のため理解が足りていない点は否めず恐縮ではありますが、
63
99
  ご助力いただきたくよろしくお願いいたします。
64
100
 
101
+