Error when calling the second activity using a button - android-studio

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?

Related

When I click on Forgot password app crash

This is the code:
class MainActivity : AppCompatActivity() {
lateinit var txtForgotPassword: TextView
lateinit var register: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
txtForgotPassword = findViewById(R.id.frgtpass)
txtForgotPassword.setOnClickListener {
val intent=Intent(this#MainActivity,Register::class.java)
startActivity(intent)
}
}
}
This is the error I faced:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vishesh.foodrunner/com.vishesh.foodrunner.Register}: java.lang.NullPointerException
From what I can see from the logs you have a property in MainActivity line 17 which you assigned lateinit property but forgot to initialize it. I am not sure if you used it or not but give a base value to it and the error will go away you can always change as it is a var variable.

How to pass array list data to a fragment from main activity when using bottom navigation view and Android Navigation component?

I am trying to pass array list data to one of my fragments in the nav bar from main activity. Is there a simple way to implement this? Or should I use activity instead of the fragment? I need to pass the array list data to my HomeFragment and use recycler view adapter in the fragment, but now the issue is I don't know how to pass data because I am using bottom navigation view and Android Navigation component.
Main Activity
package com.example.a5t1v2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.widget.GridView
import androidx.navigation.NavArgument
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
private lateinit var bottomNavigationView: BottomNavigationView
private lateinit var navController: NavController
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var shoppingItemList:MutableList<Item>
private lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initUI()
val dbHelper = DBHelper(this)
populateDB(dbHelper)
shoppingItemList = dbHelper.getAlItems()
}
private fun populateDB(dbHelper: DBHelper) {
dbHelper.insertItem(Item("Bread",1,"Default", urgent = true, bought = false,null))
dbHelper.insertItem(Item("Chocolate Bar",1,"Small", urgent = false, bought = false,null))
dbHelper.insertItem(Item("Instant noodle",1,"Default", urgent = false, bought = false,null))
dbHelper.insertItem(Item("Juice",2,"Large", urgent = false, bought = false,null))
dbHelper.insertItem(Item("Milk",3,"Large", urgent = true, bought = false,null))
dbHelper.insertItem(Item("Shampoo",1,"Small", urgent = false, bought = true,"24 Aug 2020"))
dbHelper.insertItem(Item("Shower Gel",1,"Large", urgent = false, bought = true,"24 Aug 2020"))
}
private fun initUI() {
appBarConfiguration = AppBarConfiguration(setOf(R.id.urgentListFragment, R.id.homeFragment, R.id.completedListFragment))
bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
navController = findNavController(R.id.fragmentContainerView)
setupActionBarWithNavController(navController,appBarConfiguration)
bottomNavigationView.setupWithNavController(navController)
}
}
An easy and good way would be to use a sharedViewModel.
Create a ViewModel and declare a LiveData in it.
class MainViewModel: ViewModel () {
private val arrayListLiveData = MutableLiveData<ArrayList<Item>>()
}
Inside Activity, create an instance of this ViewModel in the acitivity, and you can send your arrayList from here using post on the liveData
class MainActivity : AppCompatActivity() {
private lateinit var mainViewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
mainViewModel.arrayListLiveData.post(YOUR_ARRAY_LIST_DATA)
}
}
You can observe the same liveData inside the fragment, and would receive the posted arrayList there, from the Activity
class TestFragment : Fragment(R.layout.test) {
private lateinit var mainViewModel: MainViewModel
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mainViewModel = ViewModelProvider(requireActivity()).get(MainViewModel::class.java)
mainViewModel.arrayListLiveData.observe(viewLifecycleOwner) { arrayList ->
//Perform desired operation with the array list
}
}
}

Unresolved Reference Java

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 .

Make a TabBar inside a specific NavBar fragment

my app use bottomNavBar with 3 fragments [Settings / Home / Gallery].
Now I'm trying to make a TabBar with 3 new Tabs inside of "Settings". [setting01 / setting02 / setting03]
I followed carefully this tutorial : https://www.youtube.com/watch?v=qfFANw7nPMU
and I used viewPager2 when the video uses viewPager.
but I'm still stuck in MainActivity.kt because for some reasons these two line (from the tutorial) didn't work for me :
viewPager2.adapter = PageAdapter(supportFragmentManager)
tabLayout.setupWithViewPager(viewPager2)
here is my entire MainActivity.kt file :
package com.example.appname
import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.appname.ui.settings.PageAdapter
import kotlinx.android.synthetic.main.fragment_settings.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_settings, R.id.navigation_swapper, R.id.navigation_gallery))
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
viewPager2.adapter = PageAdapter(supportFragmentManager)
tabLayout.setupWithViewPager(viewPager2)
}
}
I'm almost certain that this error exists because these two lines should be somewhere else because of the particular situation of my application.
To find yourself in a situation similar to mine I recommend you to start a new project under android studio using the template "Bottom Navigation Activity" and then follow the tutorial.
A friend of mine give me the solution.
Nothing new in MainActivity.kt like I was thinking.
Here is the new code to make tabBar in the Settings fragment :
settingFragment.kt
class SettingsFragmentAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
private val fragments =
listOf(
fragment_setting_teams(),
fragment_setting_general(),
fragment_setting_movies()
)
override fun getItemCount(): Int = fragments.size
override fun createFragment(position: Int): Fragment = fragments[position]
}
class SettingsFragment : Fragment() {
private lateinit var settingsViewModel: SettingsViewModel
private lateinit var settingsAdapter: SettingsFragmentAdapter
private lateinit var viewPager: ViewPager2
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val tabLayout = view.findViewById<TabLayout>(R.id.tabLayout)
settingsAdapter =
SettingsFragmentAdapter(
this
)
viewPager = view.findViewById(R.id.viewPager2)
viewPager.adapter = settingsAdapter
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
when (position) {
0 -> tab.text = "Teams"
1 -> tab.text = "General"
2 -> tab.text = "Movies"
}
}.attach()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
settingsViewModel =
ViewModelProviders.of(this).get(SettingsViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_settings, container, false)
return root
}
}

How to pass string from activity to another activity in kotlin

I am trying to pass over a text to my second activity but it does not seem to be working correctly.
For my main activity i have a a textview and the text says 'Cat'. When the image on the first activity is pressed on, I want "Cat" to be passed over to the second activity. The second activity's textview should also display "Cat", however when I do this, it will show up as "Info(name=Cat)" rather then just "Cat".
I am not sure where i went wrong.
This is my main activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val image = findViewById<ImageView>(R.id.imageView)
val text = findViewById<TextView>(R.id.textView)
image.setOnClickListener {
val intent= Intent(this, MainActivity2::class.java).apply {
putExtra("name", Info("Cat"))
}
startActivity(intent)
}
}
}
My parcelable class
#Parcelize
data class Info(val name : String) : Parcelable {
}
This is my second activity
class MainActivity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
val info = intent.getParcelableExtra<Info>("name")
val name = findViewById<TextView>(R.id.textView2)
name.text = info.toString()
}
}
You're converting the whole Info data class to its string representation. You only want the name property from the data class.
name.text = info.name

Resources