【问题标题】:findViewById error - Not enough information to infer type variable T. I copied java code and converted it onlinefindViewById 错误 - 没有足够的信息来推断类型变量 T。我复制了 java 代码并在线转换了它
【发布时间】:2020-12-23 16:32:00
【问题描述】:

从 diolor/swipecards (GITHUB) 复制的 java 代码。使用 help 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)

【问题讨论】:

    标签: android android-studio kotlin findviewbyid


    【解决方案1】:

    我相信您由于代码转换而面临的问题。 Java 不要求您显式地转换视图,而 Kotlin 要求您指定视图的类型。您需要像这样在尖括号内设置视图

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

    【讨论】:

      【解决方案2】:

      您使用 findViewById 获得的类型可能不受约束,因此无法推断类型,并且在 Kotlin 中需要显式声明/转换。

      如果您在应用中针对 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(...)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-11
        • 2021-02-11
        • 2017-12-29
        相关资源
        最近更新 更多