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()
'''
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 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 am learning android studio and I can't find out why it happens, I would appreciate if someone could explain me this:
When I place xxxxanything.text after the onCreate I get the error " expected member declaration" but it works inside the onCreate metod.Why does it happen ?
I saw activity life cycle some times but im still in doubt about where to put things, like onclick listener.
I were wondering at several guides already and I am working on udacity at moment, having a hard time to understand recyclerview and I also trying to develop good programing practices.
I really appreciate any help you can provide.
Works like this
package app.helloworld.dashimir.com.diceroller
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton: Button = findViewById(R.id.roll_button)
rollButton.text = "Let's Roll"
}
}
but i get error when i place it after on create : expected member declaration;
package app.helloworld.dashimir.com.diceroller
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
val rollButton: Button = findViewById(R.id.roll_button)
rollButton.text = "Let's Roll"
}
In kotlin (.kt) files your code should be inside a function (normally main). In Android development this means that usually your code will be inside methods like the onCreate from an activity, but other methods from classes or top level functions (outside of a class) work as well.
In your example the code you moved outside the onCreate includes a value declaration val rollButton: Button = findViewById(R.id.roll_button) which is valid inside the body of a class since it will be turned into a property of said class. But the second line: rollButton.text = "Let's Roll" is an assignment an those can only be performed inside a function.
Additionally, in Android with kotlin you have available kotlin android extensions which let you reference views from the xml directly using their id without the need of using findViewById
One of best ways it build out function.
first: add OnClick to button in xml file:
android:OnClick="OnClick"
second: build Onclick function in MainActivity class and don't forget the constructor (v:View):
package app.helloworld.dashimir.com.diceroller
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun OnClick(v:View){
val rollButton: Button = findViewById(R.id.roll_button)
rollButton.text = "Let's Roll"
}
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