How to remove error "Unresolved reference text" in kotlin - android

I am trying to create a basic android project,but i am getting a constant error "Unresolved reference text".How to resolve it?
package com.example.shashank.simpson2
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.example.shashank.simpson2.R.id.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun change(view: View)
{
val x=Simpson(nameText.text.toString(), Integer.parseInt(ageText.text.toString()), jobText.text.toString())
textView.text="Name "+x.name+"Age "+x.age+"Job "+x.job
}

What you want to achieve here is to reference the view by its id in your kotlin code. You should check if you've added all the necessary plugins and implementations in your gradle script. This link thoroughly explains your problem's solution.

Related

Kotlin: Unresolved reference: Textview

I am new to Kotlin and android in general.
Currently making an app that is supposed to calculate a persons age in minutes, however Im getting an error saying "Unresolved reference: TextView". The thing is, as you can see, I've already imported widget.Textview.
Please help... I'm stuck on such a trivial problem. Thank you.
P.S. There have been similar questions, but the solutions either aren't applicable or have already been applied.
import android.app.DatePickerDialog
import android.content.ReceiverCallNotAllowedException
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Button
import android.widget.Toast
import java.util.*
class MainActivity : AppCompatActivity() {
private var tvSelectedDate : Textview? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btndate : Button = findViewById(R.id.btndate)
tvSelectedDate = findViewById(R.id.tvSelectedDate)
btndate.setOnClickListener {
clickDatePicker()
}
}
...
I believe this is a simple typo:
private var tvSelectedDate : Textview? = null
You have written Textview, but should have written TextView (with a captial "V").
Simply using databinding to get view id and handle alot of issues
Data binding

The `inflate` of layoutInflater is showing as unresolved when trying to bind - Kotlin, Android Studio

I'm trying to use binding to get an id, but when I setup my binding line binding = ActvityMainBinding.inflate(layoutInflater) the inflate is red and saying that it is an unresolved reference. It gives different options for importing, but none of those options resolve the issue. They just import something and inflate is still red
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import com.example.postrequestspike.databinding.ActivityMainBinding
const val BASE_URL = "https://jsonplaceholder.typicode.com/"
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActvityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
getMyData()
}
Here are some screenshots:
inflate shows unresolved reference
options are shown after clicking import
options import but do not resolve the reference
Well, in this case I made a spelling error. (See binding = ActvityMainBinding.inflate(layoutInflater))

Unable to pinpoint error for unresolved reference for QualtricsFeedback Activity? using Kotlin in Android Studios

I am having an unresolved reference error in my class when building this code. I need help to pinpoint the error.
This is the error
e: .../app/src/main/java/com/example/auth_onboarding/QualtricsFeedback.kt: (37, 50): Unresolved reference: #QualtricsFeedback
(37,50) refers to the last line where this#QualtricsFeedback was called.
This is the code
package com.example.auth_onboarding
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.qualtrics.digital.IQualtricsCallback
import com.qualtrics.digital.Qualtrics
import com.qualtrics.digital.TargetingResult
class QualtricsFeedback : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_qualtrics_feedback)
setSupportActionBar(findViewById(R.id.toolbar))
Qualtrics.instance().initialize("some_key", "some_other_key", "some_other_key_2", this);
val fab =
findViewById<View>(R.id.fab) as FloatingActionButton
fab.setOnClickListener {view ->
fun onClick(view: View?) {
Qualtrics.instance().evaluateTargetingLogic(MyCallback())
}
}
}
private class MyCallback : IQualtricsCallback {
override fun run(targetingResult: TargetingResult) {
if (targetingResult.passed()) {
Qualtrics.instance().display(this#QualtricsFeedback)
}
}
}
}
I've tried .display(QualtricsFeedback.this) but that doesnt work. I've also tried just .display(QualtricsFeedback).
Are there any suggestions of what I could try?
Change to this, you should add inner to class in Kotlin it means an inner class can reference to the outer class
private inner class MyCallback : IQualtricsCallback {
override fun run(targetingResult: TargetingResult) {
if (targetingResult.passed()) {
Qualtrics.instance().display(this#QualtricsFeedback)
}
}
}

How do I fix these 5 Android SDK compilation errors?

I am following this viewflipper tutorial. After the build I am receiving the following errors:
Unresolved reference: activity_main
Unresolved reference: view_flipper
Smart cast to 'ViewFlipper!' is impossible, because 'viewFlipper' is a mutable property that could have been changed by this time
Smart cast to 'ViewFlipper!' is impossible, because 'viewFlipper' is a mutable property that could have been changed by this time
Smart cast to 'ViewFlipper!' is impossible, because 'viewFlipper' is a mutable property that could have been changed by this time
Listed below is the code to my MainActivity.kt
'''
package com.example.mobilecop
import android.R
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.TextView
import android.widget.ViewFlipper
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private var viewFlipper: ViewFlipper? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewFlipper = findViewById(R.id.view_flipper)
val textView = TextView(this)
textView.text = "Dynamically added TextView"
textView.gravity = Gravity.CENTER
viewFlipper.addView(textView)
viewFlipper.setFlipInterval(2000)
viewFlipper.startFlipping()
}
fun previousView(v: View?) {
viewFlipper!!.setInAnimation(this, R.anim.slide_out_right)
viewFlipper!!.setOutAnimation(this, R.anim.slide_in_left)
viewFlipper!!.showPrevious()
}
fun nextView(v: View?) {
viewFlipper!!.setInAnimation(this, R.anim.slide_in_left)
viewFlipper!!.setOutAnimation(this, R.anim.slide_out_right)
viewFlipper!!.showNext()
}
}
'''
After removing import android. R, I was able to fix the first two errors. I removed the following three lines and the app runs.
'''viewFlipper.addView(textView)
viewFlipper.setFlipInterval(2000)
viewFlipper.startFlipping()
'''

android error (Expecting member declaration)

I'm having issues getting past this point. when I type private EditText nameText; I don't get an import and the application acts like it won't recognize the command.
package com.chriskehl.storybook
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class storyBook : AppCompatActivity() {
private EditText nameField; // expecting member declaration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_story_book)
}
}
import EditText manually import android.widget.EditText;
then clean your project
option Build->clean

Categories

Resources