class MainActivity : AppCompatActivity() {
lateinit var playbutton : Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
playbutton = findViewById(R.id.play_button)
playbutton.setOnClickListener {
val intent = Intent(this,Frame::class.java)
startActivity(intent)
}
}
}
Showing Error Unreolved Reference Java
my Imports :
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
I am starting another Activity From The Mainactivity and android studio is Showing This Error .
Related
I am trying to communicate between MainActivity.kt and sharedviewmodel.kt which is of type viewModel() for WearOS, But when i try to create an instance of the sharedviewmodel.kt in MainActivity.kt by below lines
private lateinit var viewModel: sharedviewmodel
viewModel = ViewModelProvider(this).get(sharedviewmodel::class.java)
when i pass "this" as owner of the ViewModelProvider, i face a error called
Required : ViewModelStoreOwner
Found : MainActivity
Kindly someone help to solve the issue,
Find the MainActivity.kt and sharedviewmodel.kt below.
import android.app.Activity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.watch7_view_model_example.databinding.ActivityMainBinding
class MainActivity : Activity() {
private lateinit var binding: ActivityMainBinding
private lateinit var viewModel: sharedviewmodel
var tag = "view_model"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
viewModel = ViewModelProvider(this).get(sharedviewmodel::class.java)
viewModel.azimuth().observe(this, Observer {
Log.d(tag,"data")
})
}
}
sharedviewmodel.kt
package com.example.watch7_view_model_example
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class sharedviewmodel:ViewModel() {
private val azimuth: MutableLiveData<String> = MutableLiveData()
private val elevation: MutableLiveData<String> = MutableLiveData()
fun azimuth(): LiveData<String> = azimuth
fun elevation(): LiveData<String> = elevation
fun azimuth(azimuth_t: String) {
azimuth.value = azimuth_t
}
fun elevation(elevation_t: String) {
elevation.value = elevation_t
}
init {
// Setting default value
azimuth.value = "0"
elevation.value = "0"
}
}`
I am new to Kotlin and Android Studio. I tried implementing Fragments and Navigation in my application. Following is my code. When the Application is run, it keeps on crashing. I don't understand where I ve made mistake. The application does not show any compilation error.
MainActivity.kt :
package com.example.firstandroid
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.firstandroid.databinding.ActivityMainBinding
import androidx.navigation.fragment.NavHostFragment
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
// Get the navigation host fragment from this Activity
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
// Instantiate the navController using the NavHostFragment
val navController = navHostFragment.navController
// Make sure actions in the ActionBar get propagated to the NavController
}
StartOrder.kt :
package com.example.firstandroid
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.example.firstandroid.databinding.FragmentStartOrderBinding
class StartOrder : Fragment() {
private var binding: FragmentStartOrderBinding?=null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding=FragmentStartOrderBinding.inflate(inflater, container, false)
return binding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding?.StartOrderButton?.setOnClickListener{orderDish()}
}
override fun onDestroyView() {
super.onDestroyView()
binding = null
}
fun orderDish(){
findNavController().navigate(R.id.action_startOrder_to_chooseDish)
}
}
chooseDish.kt :
package com.example.firstandroid
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.firstandroid.databinding.FragmentChooseDishBinding
class ChooseDish : Fragment() {
private var binding: FragmentChooseDishBinding?=null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding= FragmentChooseDishBinding.inflate(inflater, container, false)
return binding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding?.chooseDishNext?.setOnClickListener{orderSidedish()}
}
override fun onDestroyView() {
super.onDestroyView()
binding = null
}
fun orderSidedish(){
findNavController().navigate(R.id.action_chooseDish_to_sidedish)
}
}
Try this Solution:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.firstandroid.databinding.ActivityMainBinding
import androidx.navigation.fragment.NavHostFragment
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
}
}
basically i am trying to create a recycler view and this the main activity
i am getting error at
recyclerView.adapter = adapter
package com.example.newsorning
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.RecyclerView
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView.layoutManager = LinearLayoutManager(this)
val items = fetchData()
val adapter = NewsListAdapter(items)
recyclerView.adapter = adapter
}
private fun fetchData():ArrayList<String>{
val list = ArrayList<String>()
for (i in 0 until 100){
list.add("item $i")
}
return list
}
}
this is where i created adpater
//newsListadapter
package com.example.newsorning
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
class NewsListAdapter(private val item:ArrayList<String>):
RecyclerView.Adapter<NewsViewHolder>() {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): NewsViewHolder {
val view = LayoutInflater.from(p0.context).inflate(R.layout.item_news,p0,false)
return NewsViewHolder(view)
}
override fun getItemCount(): Int {
return item.size
}
override fun onBindViewHolder(p0: NewsViewHolder, p1: Int) {
val currentItem = item[p1]
p0.titleView.text = currentItem
}
}
class NewsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val titleView : TextView = itemView.findViewById(R.id.titleView)
}
This may be due to you using the old, deprecated support library RecyclerView (android.support.v7) instead of the Jetpack RecyclerView (androidx). Maybe one of the libraries you're using still uses the old support library, so it was in your namespace and you accidentally selected it when choosing which class to import. You need to change the line
import android.support.v7.widget.RecyclerView
to
import androidx.recyclerview.widget.RecyclerView
in both of these files. And you need to enable Jetifier according to these directions.
I have a problem calling a second activity using a password recovery button. I get the app stopped.
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun goToRecover(view: View) {
val intent = Intent(this,RecoverPasswordActivity::class.java)
this.startActivity(intent)
}
}
Did you try adding the onClick in the XML-layout like this?
android:onClick="/*your method name*/"
If so, make sure that there are no typos. In your case it would probably be:
android:onClick="addOne"
OR
You can also listen for the click events programmatically. like below:
set the Button in OnCreate method:
var goToRecoverBtn = findViewById(R.id./*id name here*/)
after that add the ClickListener to the goToRecoverBtn:
goToRecoverBtn.setOnClickListener{
// do anything whatever you want
}
OR
MainActivity code looks like below
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var goToRecoverBtn = findViewById(R.id./*id name here*/)
goToRecoverBtn.setOnClickListener{
val intent = Intent(this#MainActivity, RecoverPasswordActivity::class.java)
startActivity(intent)
}
}
}
BTW could you please show us your .xml file?
My android emulator should destroy the contents of textView widget when I turn it to landscape from portrait. But it isn't doing that, WHY? Isn't it destroying the activity?
look in the emulator "this is the first text"-- I added this line in emulator when it was in portrait mode.
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private var textView: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//comment
val userInput: EditText = findViewById<EditText>(R.id.editText)
val button: Button = findViewById<Button>(R.id.button)
textView = findViewById<TextView>(R.id.textView)
textView?.text = ""
textView?.movementMethod =
ScrollingMovementMethod() // It allows my text view widget to scroll upward
userInput.text.clear()
button.setOnClickListener(object : View.OnClickListener {
override fun onClick(p0: View?) {
textView?.append(userInput.text)
textView?.append("\n")
userInput.setText("")
}
})
}
}
```[enter image description here][1]
[1]: https://i.stack.imgur.com/oLRr3.png