I don't know why text is not changing in the BirthdayGreetingActivity.
No error is coming but it's not changing text. I checked(by Logcat) that name has came successfully to BirthdayGreetingActivity.kt from MainActivity.kt.
I guess that this [binding.birthdayGreeting.text = "Happy Birthday $name"] line in BirthdayGreetingActivity.kt is not working properly.
MainActivity.kt --
package com.example.firstapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.example.firstapp.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
fun createBirthdayCard(view: View){
val name = binding.nameInput.editableText.toString()
// Toast.makeText(this,"Name is $name",Toast.LENGTH_SHORT).show()
val intent = Intent(this,BirthdayGreetingActivity::class.java)
intent.putExtra(BirthdayGreetingActivity.NAME_EXTRA,name)
startActivity(intent)
}
}
BirthdayGreetingActivity.kt --
package com.example.firstapp
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.firstapp.databinding.ActivityBirthdayGreetingBinding
class BirthdayGreetingActivity : AppCompatActivity() {
private lateinit var binding: ActivityBirthdayGreetingBinding
companion object{
const val NAME_EXTRA ="name_extra"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBirthdayGreetingBinding.inflate(layoutInflater)
setContentView(R.layout.activity_birthday_greeting)
val name = intent.getStringExtra(NAME_EXTRA)
// val greet = binding.birthdayGreeting.editableText.toString()
// print(name)
// if (name != null) {
// Log.d("greet = ",binding.birthdayGreeting.editableText.toString())
// }
binding.birthdayGreeting.text = "Happy Birthday $name" [maybe something wrong here]
}
}
:) :) Thanks for spending your valuable time to help me :) :)
On BirthdayGreetingActivity you are passing the layout ID for setContentView, instead you must pass the binding.root, just like you did on MainActivity...
Change this line:
setContentView(R.layout.activity_birthday_greeting)
To:
setContentView(binding.root)
Related
If I should change it from card to maybe a button for easier code please let me know, but I have tried multiple ways with card view and none seem to be working for me. Im trying to open a new activity when i click the cardview for DashboardTmsActivity.
current error is this
MainActivity.kt
Modifier 'override' is not applicable to 'local function':19
Unresolved reference: activity
Unresolved reference: activity:23
here is my current code.
MainActivity.kt
package com.cameron.armymaintenance
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.cardview.widget.CardView
import com.cameron.armymaintenance.databinding.ActivityDashboardTmsBinding
import com.cameron.armymaintenance.databinding.ActivityMainBinding
class MainActivity : DrawerBaseActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
view.findViewById<CardView>(R.id.card1).setOnClickListener {
val card1 = Intent(activity, ActivityDashboardTmsBinding::class.java)
activity?.startActivity(card1)
}
}
}
}
DrawerBaseActivity.kt
package com.cameron.armymaintenance
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.FrameLayout
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView
open class DrawerBaseActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
var drawerLayout: DrawerLayout? = null
override fun setContentView(view: View?) {
drawerLayout = layoutInflater.inflate(R.layout.activity_drawer_base, null) as DrawerLayout
val container = drawerLayout!!.findViewById<FrameLayout>(R.id.activityContainer)
container.addView(view)
super.setContentView(drawerLayout)
val toolbar = drawerLayout!!.findViewById<Toolbar>(R.id.toolBar)
setSupportActionBar(toolbar)
val navigationView = drawerLayout!!.findViewById<NavigationView>(R.id.nav_view)
navigationView.setNavigationItemSelectedListener(this)
val toggle = ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.menu_drawer_open,
R.string.menu_drawer_open
)
drawerLayout!!.addDrawerListener(toggle)
toggle.syncState()
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
return false
}
private fun allocateActivityTitle(titleString: String?) {
if (supportActionBar != null) {
supportActionBar!!.title = titleString
}
}
open fun onViewCreated(view: View, savedInstanceState: Bundle?) {}
}
DashboardTmsActivity.kt
package com.cameron.armymaintenance
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class DashboardTmsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard_tms)
}
}
So there should be 2 errors if I have not misinterpreted:
Modifier 'override' is not applicable to 'local function':19
Unresolved reference: activity:23
onViewCreated() should not be placed inside onCreate(). If you need to implement onViewCreated(), it should be at the same level with onCreate()
activity is not available in an Activity. It only exists in a Fragment class. You can simply use this in an Activity.
class MainActivity : DrawerBaseActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
// Move to same level for function
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
view.findViewById<CardView>(R.id.card1).setOnClickListener {
// Use this in Activity
val card1 = Intent(this#MainActivity, ActivityDashboardTmsBinding::class.java)
startActivity(card1)
}
}
}
why can't I access a variable from another class? (the variable is in the data class, and when I want to access it, it throws "unresolved reference")
Here's what It looks like:
The code that tries to access the variable:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class questionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_question)
var nameDisplay: TextView = findViewById(R.id.nameDisplay)
var name2 = usernameStore.username //here's the error, the "username" thing is red
nameDisplay.setText(name2)
}
}
The data Class:
package com.ketchup.myquizzies
public data class usernameStore (
var username: String
)
Any help appreciated guys, I literally searched all things that came to my mind to solve this, but I couldn't help myself :(
Android Studio, Kotlin
usernameStore is a class, not an object. You need to create an instance of usernameStore to be able to use it.
class questionActivity : AppCompatActivity() {
val store = usernameStore()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_question)
var nameDisplay: TextView = findViewById(R.id.nameDisplay)
var name2 = store.username
nameDisplay.setText(name2)
}
}
FWIW, creating instances of classes is covered by this section of this chapter of this free book.
You have not created instance of class
usernameStore
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class questionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_question)
var nameDisplay: TextView = findViewById(R.id.nameDisplay)
// replace with below code
var name2 = usernameStore().username
nameDisplay.setText(name2)
}
}
Please help me figure out this Unresolved Error.
The code below gives an
Unresolved reference: text
below code is of
birthdayGreeting.kt
package com.example.birthdaygreet
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class BirthdayGreeting : AppCompatActivity() {
companion object{
const val NAME_EXTRA = "name_extra"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_birthday_greeting)
val name = intent.getStringExtra(NAME_EXTRA)
BirthdayGreeting.text="happyBirthday$name"
}
}
The error according to android studio is in
birthdayGreeting.text = "Happy Birthday $name"
Here is code of main activity
package com.example.birthdaygreet
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun createBirthdayCard(view: View) {
val name =nameInput.editableText.toString()
val intent = Intent(this,BirthdayGreeting::class.java)
intent.putExtra(BirthdayGreeting.NAME_EXTRA,name)
startActivity(intent)
}
}
You are using the wrong synthetic import for the BirthdayGreeting class. You should use kotlinx.android.synthetic.main.activity_birthday_greeting.* instead of the kotlinx.android.synthetic.main.activity_main.*
package com.example.birthdayapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.example.birthdayapp.databinding.ActivityMainBinding
class birthdayActivity : AppCompatActivity() {
private lateinit var binding: birthdayActivity
//using companion object
companion object {
const val NAME_EXTRA="Name_extra"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//implementing view binding
binding= birthdayActivity.inflate(layoutInflater) //here red line shows below inflate
setContentView(binding.root) //here red line shows below root
//getting name from main activity
val name=intent.getStringExtra(NAME_EXTRA)
}
}
//MainActivity
package com.example.birthdayapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.birthdayapp.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityMainBinding.inflate(layoutInflater) //Here, I have successfully used viewbinding
setContentView(binding.root)
binding.birthdaybutn.setOnClickListener {
val name=binding.nam.editableText.toString()
val intent=Intent(this,birthdayActivity::class.java)
intent.putExtra(birthdayActivity.NAME_EXTRA,name)
startActivity(intent)
}
}
}
I have successfully used viewbinding in MainActivity but when I tried to use viewbindig in birthdayActivity , i got red lines for ( inflate and root keyword ) ,because i have used companion object in birthdayActiviy whereas I have not companion object in MainActiviy
I add a textView in layout but when I'm trying to use Kotlin Android Extensions but I get Unsolved reference error on my TextView:
package com.normal.ff
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
fun first(a:Int, b:Int){
textView.text
}
Add import kotlinx.android.synthetic.main.activity_main.* at the top of your activity.
You are almost there, you just need a reference from layout file for that text view.
something like below
class MainActivity : AppCompatActivity() {
lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById<TextView>(R.id.textView)
}
}
fun first (a:Int, b:Int){
val text = textView.text
}