Compiler breaks on try, catch in android inside extension function


#1

The error happens if you use try,catch inside btn.setOnClickListener on 123.67

``


  public fun handleDownloadRecursiveOpml(){
  var btn = findView<Button>(R.id.tryout_download_recursive_opml_btn)
  btn.setOnClickListener {
           var req: String?
           val url = “http://opmlviewer.com/Content/Directories.opml

           try{
           req = HttpRequest.get(url)?.body()
           }
           catch(e: HttpRequestException){
           var ex = e.getCause()
           Log.d(TAG, “Error in downloading OPML $url”)
           toastee(“Error in downloading OPML from $url”)
           }

           val opml = transformFromXml(req)

           if(opml.isTrue()){
           toastee(“Opml parsing is Great”)
           }   else{
           toastee(“Error in parsing opml ${opml.exception?.getMessage()}”)
           }
  }
  }

public fun OnClickListener(action: (View?) -> Unit): OnClickListener {
  return object : OnClickListener {
  public override fun onClick(p0: View?) {
           action(p0)
  }
  }
}

public fun View.setOnClickListener(action: (View?) -> Unit): Unit {
  setOnClickListener(OnClickListener(action))
}

Error:[AndroidRivers] EXCEPTION FROM SIMULATION: Error:[AndroidRivers] local 0002: invalid Error:[AndroidRivers] ...at bytecode offset 00000063 Error:[AndroidRivers] locals[0000]: Lcom/silverkeytech/android_rivers/TryOutActivity$handleDownloadRecursiveOpml$1; Error:[AndroidRivers] locals[0001]: Landroid/view/View; Error:[AndroidRivers] locals[0002]: <invalid> Error:[AndroidRivers] locals[0003]: Ljava/lang/String; Error:[AndroidRivers] locals[0004]: <invalid> Error:[AndroidRivers] locals[0005]: <invalid> Error:[AndroidRivers] stack[top0]: Lcom/silverkeytech/android_rivers/TryOutActivity; Error:[AndroidRivers] ...while working on block 0063 Error:[AndroidRivers] ...while working on method invoke:(Landroid/view/View;)V Error:[AndroidRivers] ...while processing invoke (Landroid/view/View;)V Error:[AndroidRivers] ...while processing C:/Users/Dody/Documents/GitHub/AndroidRivers/out/production/AndroidRivers/com/silverkeytech/android_rivers/TryOutActivity$handleDownloadRecursiveOpml$1.class Error:[AndroidRivers] 1 error; aborting


#2

It works fine if I don't use the extension function

``

public fun handleDownloadRecursiveOpml(){
  var btn = findView<Button>(R.id.tryout_download_recursive_opml_btn)

  &nbsp;&nbsp;btn.setOnClickListener(object: OnClickListener{

           public override fun onClick(p0: View?) {
           var req: String? = “”
           val url = “http://opmlviewer.com/Content/Directories.opml

           try{
                   req = HttpRequest.get(url)?.body()
           }
           catch(e: HttpRequestException){
                   var ex = e.getCause()
                   Log.d(TAG, “Error in downloading OPML $url”)
                   toastee(“Error in downloading OPML from $url”)
           }

           val opml = transformFromXml(req)

           if(opml.isTrue()){
                   toastee(“Opml parsing is Great”)
           }   else{
                   toastee(“Error in parsing opml ${opml.exception?.getMessage()}”)
           }
           }
  })
  }


#3

Thank you for your report! There is a bug in kotlin: missed error about "initialized variable":

``

var req: String?
val url = “<a href=“http://opmlviewer.com/Content/Directories.opml”>http://opmlviewer.com/Content/Directories.opml”

try{
  req = HttpRequest.get(url)?.body()
}
catch(e: HttpRequestException){
  var ex = e.getCause()
  Log.d(TAG, “Error in downloading OPML $url”)
  toastee(“Error in downloading OPML from $url”)
}

val opml = transformFromXml(req) // Should be an error here (Variable ‘req’ must be initialized)


So you can use try…catch in extension function, but you have to initialize ‘req’ variable before block, in finally block or in both branches (try, catch).