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
Related
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
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))
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()
'''
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.
I have the following code in Kotlin
package com.example.android.navigation
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI
import com.example.android.navigation.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val navcon = this.findNavController(R.id.NavFragment)
NavigationUI.setupActionBarWithNavController(this, navcon)
}
override fun onNavigateUp(): Boolean {
val navcon = this.findNavController(R.id.NavFragment)
return navcon.navigateUp()
}
}
The code is supposed to implement the back button and make it work correctly (navigate up the back stack). However, when I run the application and click on the generated back button, nothing happens (the button doesn't work, but also no error is produced when clicked). Does anyone have any idea as to why it doesn't work / how to make it work properly?
AppCompatActivity has a separate onSupportNavigateUp() method, which should be used instead of onNavigateUp() as per the Navigation Action Bar documentation.