Varargs Kotlin Java互操作不正常

对于makeSceneTransitionAnimation,有两个静态函数

public static ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity, View sharedElement, String sharedElementName) 

  public static ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity, Pair<View, String>... sharedElements) 

第一个函数调用在Kotlin中正常工作,但在调用第二个函数时,这两个调用都会返回错误

  val imageTransition = Pair<View, String>(imageView, imageView.getTransitionName()); val textTransition = Pair<View, String>(textView, textView.getTransitionName()); val transitionList = Array(2, { imageTransition }); transitionList[1] = textTransition; val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, *transitionList); 

  val imageTransition = Pair<View, String>(imageView, imageView.getTransitionName()); val textTransition = Pair<View, String>(textView, textView.getTransitionName()); val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, imageTransition, textTransition); 

有没有一个适当的方式来得到这个工作或这是一个互操作问题?

编辑添加更改以确保它使用相同的类

 val imageView : View = view.findViewById(android.R.id.icon); val textView : View = view.findViewById(android.R.id.text1); imageView.setTransitionName("imageTransition"); textView.setTransitionName("textTransition") val imageTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(imageView, imageView.getTransitionName() as java.lang.String); val textTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(textView, textView.getTransitionName() as java.lang.String); val transitionList = Array(2, { imageTransition }); transitionList[1] = textTransition; val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, *transitionList); 

当前编译器错误:

 Error:(72, 84) The spread operator (*foo) may only be applied in a vararg position Error:(72, 99) No value passed for parameter sharedElementName 

而另一个

 val imageView : View = view.findViewById(android.R.id.icon); val textView : View = view.findViewById(android.R.id.text1); imageView.setTransitionName("imageTransition"); textView.setTransitionName("textTransition") val imageTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(imageView, imageView.getTransitionName() as java.lang.String); val textTransition : android.support.v4.util.Pair<android.view.View, java.lang.String> = android.support.v4.util.Pair.create(textView, textView.getTransitionName() as java.lang.String); val options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, imageTransition, textTransition); 

当前编译器错误:

 Error:(72, 84) Type mismatch: inferred type is android.support.v4.util.Pair<android.view.View, java.lang.String> but android.view.View! was expected Error:(72, 101) Type mismatch: inferred type is android.support.v4.util.Pair<android.view.View, java.lang.String> but kotlin.String! was expected 

答案是数组变量前面的*符号:

 import android.support.v4.util.Pair as AndroidPair // ... val pair1 = AndroidPair<View, String>(fab, getString(R.string.transition_fab)) val pair2 = AndroidPair<View, String>(findViewById(R.id.app_bar), getString(R.string.transition_appbar)) ActivityOptionsCompat.makeSceneTransitionAnimation(this@MyActivity, *arrayOf(pair1, pair2)).toBundle(); 

这可能是你不小心使用kotlin.Pair而不是android.util.Pair 。 请将以下导入指令添加到文件的开头:

 import android.util.Pair 

这对我工作:

 import android.support.v4.util.Pair // ... val options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, Pair<View, String>(image, image.transitionName), Pair<View, String>(title, title.transitionName)) startActivity(intent, options.toBundle())