【问题标题】:how to force a method to infer a generic type如何强制方法推断泛型类型
【发布时间】:2019-07-08 14:01:15
【问题描述】:

在下面的代码中,我有 2 个片段 FragmentLeft 和 FragmentRight 都从 Fragment 类扩展。 根据下面的扩展方法,我正在尝试使用通用数据类型作为方法的参数,因此当我使用此方法时,它应该接受 FragmentLeft 或 FragmentRight 的实例.....

但是,下面的代码会产生注释中所述的错误

请告诉我如何解决它

代码

var fragTransactionInstance = getFragmentManagerInstance()?.let {
        it.getFragmentTransactionInstance()
            ?.replaceTransaction(R.id.fragLeft, fragmentLeft)//no enough 
   information to infer type variable T   
            ?.replaceTransaction(R.id.fragRight, fragmentRight)////no 
   enough information to infer type variable T 
    }?.also {
        it.commit()
    }
  }

fun initViews() : Unit {
    fragmentLeft = FragmentLeft()
    fragmentRight = FragmentRight()
}
fun <T : Fragment> FragmentTransaction.replaceTransaction(layout: Int, t: 
Fragment?): FragmentTransaction {
    return this.replace(layout, t)
}

import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.kotlindynmaicfragment_v1.MainActivity
import com.example.kotlindynmaicfragment_v1.R
import kotlinx.android.synthetic.main.activity_main.view.*

class FragmentRight : Fragment() {

val LOG_TAG = this::class.java.name

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Log.d(LOG_TAG, "onCreate")

}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    Log.d(LOG_TAG, "onActivityCreated")

}

///////////////////
class FragmentLeft : Fragment() {

val LOG_TAG = this::class.java.name

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Log.d(LOG_TAG, "onCreate")

}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    Log.d(LOG_TAG, "onActivityCreated")

}

【问题讨论】:

    标签: android android-fragments generics kotlin


    【解决方案1】:

    我认为问题在于您将 replaceTransaction 方法定义为泛型,但您在该方法中根本没有使用 T,因此类型推断不知道要使用哪种类型T.

    给你一个更简单的例子,这会导致同样的错误:

    fun <T> foo() {
        println("Hello, world!")
    }
    
    fun main() {
        foo()
    }
    

    发生这种情况是因为无法推断出T,因此您需要明确说明要使用的类型(即使在foo 中根本没有使用它):

    fun main() {
        foo<Unit>() // I used Unit, but any other type would do
    }
    

    鉴于此,您的代码中是否需要TFragmentRightFragmentLeft 都从 Fragment 扩展而来,因此除非您需要使用这些类中的特定功能,否则您可以丢弃 T 并使用父类型 Fragment(就像您已经在做的那样)。

    【讨论】:

      【解决方案2】:

      不使用t:Fragment,而是将t的类型改为T,而不是Fragment 尝试以这种方式编写扩展函数:

      fun <T : Fragment> FragmentTransaction.replaceTransaction(layout: Int, t: 
      T): FragmentTransaction {
          return this.replace(layout, t)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-08
        • 1970-01-01
        • 2011-11-15
        • 2021-08-22
        • 1970-01-01
        • 2012-02-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多