findViewById 错误 - 没有足够的信息来推断类型变量 T。我复制了 java 代码并在线转换

findViewById error - Not enough information to infer type variable T. I copied java code and converted it online

从 diolor/swipecards (GITHUB) 复制了 java 代码。借助 pf 在线工具将其转换为 kotlin。它有一些错误已得到纠正,但最后一个错误仍然出现在 OnScroll 函数中 (findViewById)。

package com.example.fanatic

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.Toast
import com.lorentzos.flingswipe.SwipeFlingAdapterView

class Swipe_card_activity : AppCompatActivity() {


    private var al:ArrayList<String> = TODO()
    private lateinit var arrayAdapter:ArrayAdapter<String>
    private var i:Int = 0

    override fun onCreate(savedInstanceState:Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_swipe_card_activity)
        al = ArrayList()
        al.add("php")
        al.add("c")
        al.add("python")
        al.add("java")
        al.add("html")
        al.add("c++")
        al.add("css")
        al.add("javascript")


        arrayAdapter = ArrayAdapter(this, R.layout.item, R.id.helloText, al)

        val flingContainer : SwipeFlingAdapterView = findViewById<SwipeFlingAdapterView>(R.id.frame)

        flingContainer.setAdapter(arrayAdapter)
        flingContainer.setFlingListener(object: SwipeFlingAdapterView.onFlingListener {
            override fun removeFirstObjectInAdapter() {
                // this is the simplest way to delete an object from the Adapter (/AdapterView)
                Log.d("LIST", "removed object!")
                al.removeAt(0)
                arrayAdapter.notifyDataSetChanged()
            }
            override fun onLeftCardExit(dataObject:Any) {
                //Do something on the left!
                //You also have access to the original object.
                //If you want to use it just cast it (String) dataObject
                makeToast(this@Swipe_card_activity, "Left!")
            }
            override fun onRightCardExit(dataObject:Any) {
                makeToast(this@Swipe_card_activity, "Right!")
            }
            override fun onAdapterAboutToEmpty(itemsInAdapter:Int) {
                // Ask for more data here
                al.add("XML " + (i).toString())
                arrayAdapter.notifyDataSetChanged()
                Log.d("LIST", "notified")
                i++
            }

FINDVIEWBYID 此处存在错误 它说:没有足够的信息来推断 T 型。

            **override fun onScroll(scrollProgressPercent:Float) {
               strong textval viuw = flingContainer.selectedView
                viuw.run {
                    findViewById(R.id.item_swipe_right_indicator).setAlpha(if (scrollProgressPercent < 0) -scrollProgressPercent else 0)
                    findViewById(R.id.item_swipe_left_indicator).setAlpha(if (scrollProgressPercent > 0) scrollProgressPercent else 0)**
                }
            }
        })
        // Optionally add an OnItemClickListener
        flingContainer.setOnItemClickListener { itemPosition, dataObject -> makeToast(this@Swipe_card_activity, "Clicked!") }
    }
    fun makeToast(ctx: Context, s:String) {
        Toast.makeText(ctx, s, Toast.LENGTH_SHORT).show()
    }
    @OnClick(R.id.right)
    fun right() {
        /**
         * Trigger the right event manually.
         */
        val flingContainer : SwipeFlingAdapterView = findViewById<SwipeFlingAdapterView>(R.id.frame)
        flingContainer.getTopCardListener().selectRight()
    }
    @OnClick(R.id.left)
    fun left() {
        val flingContainer : SwipeFlingAdapterView = findViewById<SwipeFlingAdapterView>(R.id.frame)
        flingContainer.getTopCardListener().selectLeft()
    }

}

annotation class OnClick(val right: Int)

您使用 findViewById 获得的类型可能不受约束,因此无法推断类型,在 Kotlin 中需要明确 stated/casted.

如果您的应用目标是 API 26 岁或更高年龄,您可以:

findViewById<THE_VIEW_TYPE>(R.id.item_swipe_right_indicator).setAlpha(...)

否则 API 25 岁及以下你可以这样做:

(findViewById(R.id.item_swipe_right_indicator) as THE_VIEW_TYPE).setAlpha(...)

我认为您遇到的问题是代码转换造成的。 Java 不要求您显式转换视图,而 Kotlin 要求您指定视图类型。您需要像这样在 angular 括号内设置视图

findViewById<View>(R.id.item_swipe_right_indicator).setAlpha(...)