• 15
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Android Studio & Kotlin: unresolved reference:context

I've tried hundreds of ways to resolve this reference problem:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        val dm = DataManager()
        val adapterCourses = ArrayAdapter<CourseInfo>(context:  this,
                    android.R.layout)

but in ArrayAdapter<CourseInfo>(context: this, android.R.layout) I get unresolved reference: context and I have no idea why.

Android Studio version: 3.3.2 Kotlin version: 1.3.21

Could anyone help me?

      • 1
    • context: this isn't valid syntax. If the class is in Kotlin, you can do context = this. If it's written in Java, you can't use explicit names
      • 1
    • Thank you for the answer. You're right, I've tried to make it like this. but I thought it will not give me the same result. So in Kotlin I should avoid colon in properties and use equal sign instead?
      • 1
    • @Rybecki You CAN use the equal sign but you don't HAVE TO :) I'm following the same tutorial (PluralSight, Android Apps with Kotlin, Section 4, last demo) and know where you are coming from. This is one of the interesting cases in which the IDE (Android Studio) is smarter than us :) When you type "this," (without "context: "), Android Studio looks at the definition of ArrayAdapter and figures out that the first argument is context and might inform you about it (depending on your settings). Consequently, you can either remove "context: " (as Ryan said) or write "context = this" (as Zoe said).

I had a similar error message because I didn't import the Context. If you haven't explicitly imported Context, try adding this to your import list near the start of your Activity file:

 import android.content.Context          
  • 1
Reply Report

The column in Kotlin is used for some things, but not when passing named arguments. The syntax for passing a named parameter is parameterName = parameterValue.

When you write context = this, while passing a parameter, you are simply referring to the parameter context of the function you are calling, explicitly saying that this has to correspond to that context parameter. This is not very useful in this case unless you want to be very explicit.

The usefulness of using named arguments arise when you are dealing with optional parameters or when you are passing the parameters out of order.
E.g.

// DECLARATION of function abc
fun abc(s: String = "", i: Int = 0)

// USAGE of function abc passing only an Int
abc(i = 314)

The function abc has two parameters and they have a default value. In this case, you can avoid passing any parameter if you are fine with the defaults. But if you only want to pass i, you can do it by specifying its name, as done in the example.

Similarly, you can choose to pass parameters out of order, in that case, you'll do:

abc(i = 314, s = "something")
  • 0
Reply Report