Reputation: 55
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 :)
Upvotes: 0
Views: 51
Reputation: 19524
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
Upvotes: 1
Reputation: 411
You should change this line
val addItemSubmitButton = findViewById(R.id.addItemSubmitButton)
like below
val addItemSubmitButton = findViewById<View>(R.id.addItemSubmitButton)
Upvotes: 1