Unresolved reference: textField - kotlin

I get this message "Unresolved reference: textField" on the start of the third line on this block of code:
val items = listOf("Material", "Design", "Components", "Android")
val adapter = ArrayAdapter(requireContext(), R.layout.list_item, items)
(textField.editText as? AutoCompleteTextView)?.setAdapter(adapter)
I just copied and pasted the code directly from the material design documentation site following their instructions, but I doesn't seems to work. What am I missing here?

Related

Safaricaom Daraja API Android initialization failure (DarajaListener<AccessToken!>! was expected)

This is the way to initialize the Daraja API in Kotlin. Problem is, I get a type mismatch on the forth parameter of Dajara.with(1st, 2nd, 3rd, 4th) function. The error is at the object : DarajaListener parameter.
daraja = Daraja.with("CONSUME_KEY", "CONSUMER_SECRET_KEY", Env.SANDBOX,
**object : DarajaListener<AccessToken>** {
override fun onResult(result: AccessToken) {
Toast.makeText(applicationContext, result.access_token, Toast.LENGTH_SHORT).show()
}
override fun onError(error: String?) {
Toast.makeText(applicationContext, error.toString(), Toast.LENGTH_SHORT).show()
}
})
The build error is as follows:
Type mismatch: inferred type is but DarajaListener<AccessToken!>! was expected
Hovering the cursor around the error flashes this message:
Type mismatch.
Required:DarajaListener<AccessToken!>!Found:
Any help initializing it the right way?
It was a simple matter of imports. I had imported the wrong AccessToken. Instead of the one from the Daraja api, I imported the one provided by Facebook. Can't believe this costed me a week!

Type mismatch: inferred type is NameFragment but Context was expected - Kotlin

I want to make a record audio permission request function when someone presses a button. I've gone through many google searches from 27 errors to only 2 errors.
I've searched many questions, but none of them are a solution of my problem.
Here's the Code for fragment_edward.kt:
ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
private fun requestRecordAudioPermission(){
var permissionsToRequest = mutableListOf<String>()
if(!hasRecordAudioPermission())
permissionsToRequest.add(Manifest.permission.RECORD_AUDIO)
if(permissionsToRequest.isNotEmpty()){
ActivityCompat.requestPermissions(this, permissionsToRequest.toTypedArray(),0)
}
}
First error:
Type mismatch: inferred type is SlideshowFragment but Context was expected
second error:
Type mismarch: inferred type is SlideshowFragment but Activity was expected
I really hope, that someone can help me.
Thank you all so much for every answer or question.
I don't exactly know what this code does, but I think you are getting the errors here
ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
and here
ActivityCompat.requestPermissions(this, permissionsToRequest.toTypedArray(),0)
right?
If you are calling this code inside your fragment (which you do, because of the error messages), you should change it the following:
ActivityCompat.checkSelfPermission(requireContext(), Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
and
ActivityCompat.requestPermissions(requireActivity(), permissionsToRequest.toTypedArray(),0)

Write text and Print in Kotlin

I have the following code:
package com.zetcode
import java.io.File
fun main() {
val fileName = "P3.txt"
val content = File(P3.txt).readText()
println(content)
}
My goal is to write a code in kotlin that reads the text file (P3.txt) and prints its content. I know there is something wrong because I keep receiving "unresolved reference.
File takes a string as input, you should change the line:
val content = File(P3.txt).readText()
to
val content = File("P3.txt").readText()
The difference is that without the quotes, kotlin thinks P3 is a reference that is not declared anywhere and you get the error you've mentioned.

How to register AndroidLintInspectionBase in IntellijIdea Plugin

I'm creating a costum Android Lint Inspection and I need to register the inspection, to be run. Where do I need to register it?
I've already tried to register the inspection which provides the inspection inside plugin.xml file.
The actual inspection:
class HardcodedDimensionsInspection : AndroidLintInspectionBase("Hardcoded dimensions", HardcodedDimensDetector.ISSUE) {
override fun getShortName(): String {
return "AndroidLintHardcodedDimension"
}
}
The entry in plugin.xml file
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<!-- <inspectionToolProvider implementation="JavaInspectionProvider"/>-->
<globalInspection shortName="AndroidLintHardcodedDimension" displayName="Hardcoded dimensions"
enabledByDefault="true" level="WARNING"
implementationClass="HardcodedDimensionsInspection"/>
</extensions>
The actual detector
class HardcodedDimensDetector : LayoutDetector() {
override fun getApplicableAttributes(): Collection<String>? {
return Arrays.asList(
// Layouts
ATTR_TEXT
)
}
override fun appliesTo(folderType: ResourceFolderType): Boolean {
return (folderType == ResourceFolderType.LAYOUT ||
folderType == ResourceFolderType.MENU ||
folderType == ResourceFolderType.XML)
}
override fun visitAttribute(context: XmlContext, attribute: Attr) {
val value = attribute.value
}
companion object {
/** The main issue discovered by this detector */
#JvmField
val ISSUE = Issue.create(
id = "HardcodedDimension",
briefDescription = "Hardcoded dimens",
explanation = """
Brief
""",
category = Category.I18N,
priority = 5,
severity = Severity.ERROR,
implementation = Implementation(
HardcodedDimensDetector::class.java,
Scope.RESOURCE_FILE_SCOPE
)
)
}
}
I've expected to hit the breakpoints in any of the functions for Detector but the code is never called. Seems like my detector is not registered. Can you please point me to the missing part, is there a class where I should register my Detector?
Thank you.
The link to the full project: https://github.com/magicbytes/Android-Lint-Inspection
I don't see anything obvious wrong from these snippets. Could you please post on our forum and link to the full sources of your plugin? Thanks. https://intellij-support.jetbrains.com/hc/en-us/community/topics/200366979-IntelliJ-IDEA-Open-API-and-Plugin-Development
I have a workaround for now, not sure it's the official way to do it. Android Lint has a registry with all the Issue classes (built-in), the class is called LintIdeIssueRegistry. When it runs the Android Lint, it's looking in this registry for Issue processors. Since the list is hardcoded, we need to inject ours in the list. I'm using the following code for that:
val registry = LintIdeIssueRegistry()
val issue = registry.getIssue(HardcodedDimensDetector.ISSUE.id)
if (issue == null) {
val list = registry.issues as MutableList<Issue>
list.add(HardcodedDimensDetector.ISSUE)
}
Hopefully in future we will have a method called addIssue inside the LintIdeIssueRegistry.

IntelliJ gives error about reference in Kotlin

I'm trying to go though the Kotlin tutorial from Freecode camp, but have this issue with references.
IntelliJ complained that
Unresolved reference: greeting in sayHello() in main()
Unexpected
tokens (use ';' to separate expressions on the same line), after
"Hi"
fun sayHello(greeting:String, vararg itemsToGreet: String) {
itemsToGreet.forEach { itemToGreet ->
println("$greeting $itemToGreet")
}
}
fun main() {
val interestingThings = listOf("Kotlin", "Programming", "Comic Books")
sayHello(greeting:"Hi", itemsToGreet:"Kotlin", "Programming", "Comic Books")
}
It seems that assigning values to params can be done only by "=", not ":"
"greeting:" is a parameter name hint. Code should look like this:
sayHello("Hi", "Kotlin", "Programming", "Comic Books")
See how hints will be shown automatically.