0

i don't know why but i get this error:

e: C:\Users\User\AndroidStudioProjects\StorageManagementTheThird\app\src\main\java\com\example\storagemanagementthethird\AddItemActivity.kt: (15, 35): Type inference failed: Not enough information to infer parameter T in fun <T : View!> findViewById(p0: Int): T!

in this code:


class AddItemActivity: AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.add_item)


        val addItemSubmitButton = findViewById(R.id.addItemSubmitButton)

        addItemSubmitButton.setOnClickListener {

            val editTextItemName: EditText = findViewById(R.id.editTextItemName)

            //alles wat we aanpassen in storage en opslaan
            val database = getSharedPreferences("database", Context.MODE_PRIVATE)
            database.edit().apply {
                putString("savedItemName", editTextItemName.text.toString())
            }.apply()

        }
    }
}

please help me if you can :)

MPian
  • 55
  • 5
  • Does this answer your question? ["Not enough information to infer parameter T" with Kotlin and Android](https://stackoverflow.com/questions/45267041/not-enough-information-to-infer-parameter-t-with-kotlin-and-android) – a_local_nobody Dec 09 '20 at 19:28

2 Answers2

1

You should change this line

val addItemSubmitButton = findViewById(R.id.addItemSubmitButton)

like below

val addItemSubmitButton = findViewById<View>(R.id.addItemSubmitButton)

1

findViewById takes a type, so it can return the correct type of view - in this case you should probably use findViewById<Button> since that's what the view it meant to be, but it doesn't really matter since you can set a click listener on any type of View

If you actually specify the type on the val declaration, like val addItemSubmitButton: Button then the compiler can infer the type for the findViewById call, and you don't need the type in the diamond brackets (it'll be greyed out and you'll get a suggestion to delete it). That's why your editTextItemName lookup is fine - you're specifying the type for that one

cactustictacs
  • 17,935
  • 2
  • 14
  • 25