Skip to content

Commit

Permalink
Merge pull request #86 from omen273/59-java.lang.ClassCastException-a…
Browse files Browse the repository at this point in the history
…ndroid.widget.LinearLayout-cannot-be-cast-to-android.widget.ImageView

59 fix exception handling
  • Loading branch information
omen273 authored Feb 7, 2021
2 parents ddb2f27 + 0b0b0ab commit 6da051b
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions app/src/main/java/com/example/crosswordToLearn/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ class MainActivity : AppCompatActivity() {

private fun addItem(path: File) {
if(!path.exists()) {
Log.e("ERROR", "The path to the image doesn't exist: ${path.absolutePath}")
return
throw Exception("The path to the image doesn't exist: ${path.absolutePath}")
}
val name = path.name.removeSuffix(IMAGE_FORMAT)
val linearLayout = LinearLayout(this)
Expand All @@ -170,8 +169,7 @@ class MainActivity : AppCompatActivity() {
adjustViewBounds = true
setImageDrawable(Drawable.createFromPath(path.absolutePath))
if(drawable == null) {
Log.e("ERROR", "The bad image")
return
throw Exception("The bad image")
}
val layoutParams = LinearLayout.LayoutParams(imageSize, imageSize)
layoutParams.setMargins(MARGIN, 0, MARGIN, 0)
Expand Down Expand Up @@ -223,7 +221,11 @@ class MainActivity : AppCompatActivity() {
"exist: ${pathToCrosswordData.absolutePath}")
continue
}
addItem(file)
try {
addItem(file)
} catch (e:Exception){
Log.e("ERROR", e.message.toString())
}
}
}
}
Expand All @@ -242,7 +244,13 @@ class MainActivity : AppCompatActivity() {
Log.e("ERROR", "The path to the image doesn't exist: ${path.absolutePath}")
return
}
addItem(pathToImage(name))
try {
addItem(pathToImage(name))
}
catch (e:Exception){
Log.e("ERROR", e.message.toString())
return
}
val adapter = tableLayout.adapter as TableAdapter
val tableRow = adapter.dataset[adapter.dataset.lastIndex]
val linearLayout = tableRow[tableRow.lastIndex]
Expand Down

0 comments on commit 6da051b

Please sign in to comment.