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

里特维克

从diolor / swipecards(GITHUB)复制的Java代码。借助在线帮助工具将其转换为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++
            }

FINDVIEWBY提示错误,表示此错误:没有足够的信息来推断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)
Saksham Pruthi

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

添加横幅广告, findViewById 提供“没有足够的信息来推断类型变量”。

RxKotlin“ withLatestFrom(...)”编译错误:没有足够的信息来推断类型变量R

对于类型错误,未定义findViewById

findViewById 不断给我错误和头痛

无法在Kotlin中“ findViewById”。收到错误“类型推断失败”

没有错误,但 findviewbyid 没有解决

FindViewById()返回null,没有编译错误

没有足够的信息来推断类型变量T

R.layout 对象上的 Activity.findViewById() 错误“类型 id 的预期资源”

无法从活动错误类型静态引用非静态方法findViewById(int)

发生 findViewById 方法错误

findViewById中的错误ListView

setContentView和findViewById错误

按钮出现错误findViewById()

没有足够的信息来推断类型变量 T1

返回通用类型时*我收到错误信息,无法推断类型变量T

Rust错误:无法推断出足够的类型信息来定位特征的隐含含义

当我将由findViewById()返回的View对象作为参数传递给LinearLayout类的addView()时,为什么会发生错误?

“错误:无法解析符号 findViewById”

使用findViewById()时出现错误

类型推断失败:没有足够的信息来推断Kotlin的Paypal付款中的参数T

类型推断失败:没有足够的信息来推断参数T Kotlin

findViewById没有重复

findViewById() 更好的代码结构

没有足够的信息来推断参数T

类型推断失败:没有足够的信息来推断 fun <T : Context!> getApplicationContext(): T! 中的参数 T!请明确说明

错误:无法推断出有关_的足够类型信息;需要类型注释或通用参数绑定

AssertJ:类型推断失败:没有足够的信息来推断 org.assertj.core.api.Assertions.fail 中的参数 T

Android Studio-findViewById()导致错误