无法从匿名类访问外部类

时间:2015-12-06 15:01:36

标签: android kotlin

我无法从匿名内部访问外部方法

class MyClass()
{
    fun doSomeStuff()
    {
       for (brandView in holder.brandImages)
       {
           brandView.onClick {
               if (brandView.brandId != null)
               {
                   notifyStateChanged()
               }
           }
       }
    }
    fun notifyStateChanged()
    {
        print("something")
    }
}

我收到编译时错误:

Error:(46, 31) org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Don't know how to generate outer expression for class <closure-BrandsBarView$1>
Cause: Don't know how to generate outer expression for class <closure-BrandsBarView$1>
File being compiled and position: (46,31) in C:/Users/piotr/IdeaProjects/MerciIt/app/src/main/java/pl/com/digita/merciit/app/ui/controls/colorswitcher/brandsbar/BrandsBarView.kt
PsiElement: {
                if (brandView.brandId != null)
                {
                    notifyStateChanged()
                    //brandView.setTicked(!brandView.isTicked)
                }
            }
The root cause was thrown at: CodegenContext.java:160
    at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:299)
(...)

那么我做错了什么?

仅供理论讨论:

for (brandView in holder.brandImages)
{
    setupBrandView(brandView)
}

fun setupBrandView(brandView: BrandTickerView)
{
    brandView.onClick {brandView.isTicked = !brandView.isTicked; dataChanged?.invoke() }

}

工作正常

1 个答案:

答案 0 :(得分:6)

在匿名类import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.mllib.linalg.distributed.{MatrixEntry, CoordinateMatrix, RowMatrix} val rows= Array(((1,2,3,4,5),(1,2,3,4,5),(1,2,4,5,8),(3,4,1,2,7),(7,7,7,7,7))) val mat = new RowMatrix(rows) val simsPerfect = mat.columnSimilarities() val simsEstimate = mat.columnSimilarities(0.8) 中引用外部类。 来自this的外部活动必须明确提及

object

为了帮助您了解情况,我们很高兴看到类层次结构。