My App Crashes When I Clicked the Button Which Will Take Me the Another Activity on Java, Android Studio

My app crashes when I clicked the button which will take me the another Activity on Java, Android Studio

Change android:src="@+id/micc" to android:src="@drawable/micc" /> in ImageButton. It's causing the problem.

Android Studio Kotlin: Button continually causes crashes when clicked

I finally found the error. Your Button has the same name like the Activity that will be used for intent. Change it to this:

package com.smvcalculator

import android.content.Intent
import androidx.appcompat.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 maleLP_button = findViewById<Button>(R.id.malebtn) //new button name
maleLP_button.setOnClickListener {
val intent = Intent(this, MaleLP::class.java) //different to class name
startActivity(intent)
}
}
}

Now it should work :)

App crashes when a button is used to invoke a new activity

Update your button xml code to this :

<Button
android:text="About"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView6"
android:layout_centerHorizontal="true"
android:id="@+id/button2"
style="@style/Widget.AppCompat.Button.Borderless"
android:background="?android:attr/colorBackground"
android:backgroundTint="?android:attr/colorBackground"
android:onClick="button2" />

What above code is doing is it will trigger the method button2 of your activity. Earlier it was searching for following method name: button2 (BitRates) in your acitvity

button.setOnClickListener is crashing my app. Kotlin 1.3.72

Through the help of this post and more research I found a solution.
I knew my problem was the creating/calling of the button. My button was not located in my main activity xml file but in another. So in my adapter is where I had to put the code.

package com.dispatch.tripsheet



import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.table_list_item.view.*

class TableViewAdapter(private val movieList: List<MovieModel>) : RecyclerView.Adapter<TableViewAdapter.RowViewHolder>() {



override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RowViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.table_list_item, parent, false)
return RowViewHolder(itemView)


}

private fun setHeaderBg(view: View) {
view.setBackgroundResource(R.drawable.table_header_cell_bg)
}

private fun setContentBg(view: View) {
view.setBackgroundResource(R.drawable.table_content_cell_bg)
}

override fun onBindViewHolder(holder: RowViewHolder, position: Int) {
val rowPos = holder.adapterPosition



if (rowPos == 0) {
// Header Cells. Main Headings appear here
holder.itemView.apply {
setHeaderBg(txtWOrder)
setHeaderBg(txtDElNote)
setHeaderBg(txtCompany)
// setHeaderBg(txtAddress)
setHeaderBg(txtWeight)
setHeaderBg(txtbutton1)
setHeaderBg(txtbutton2)
setHeaderBg(txttvdone)


txtWOrder.text = "WOrder"
txtDElNote.text = "DElNote"
txtCompany.text = "Company"
// txtAddress.text = "Address"
txtWeight.text = "Weight"
txtbutton1.text = "Delivered"
txtbutton2.text = "Exception"
txttvdone.text = ""
}
} else {
val modal = movieList[rowPos - 1]

holder.itemView.apply {
setContentBg(txtWOrder)
setContentBg(txtDElNote)
setContentBg(txtCompany)
// setContentBg(txtAddress)
setContentBg(txtWeight)
setContentBg(txtbutton1)
setContentBg(txtbutton2)
setContentBg(txttvdone)

txtWOrder.text = modal.WOrder.toString()
txtDElNote.text = modal.DElNote.toString()
txtCompany.text = modal.Company.toString()
// txtAddress.text = modal.Address.toString()
txtWeight.text = modal.Weight.toString()
txtbutton1.text = modal.Button1.toString()
txtbutton2.text = modal.Button2.toString()
txttvdone.text = modal.tvdone.toString()
}
}

holder.apply {
txtbutton1.setOnClickListener {
Log.e("Clicked", "Successful delivery")
txttvdone.setBackgroundResource(R.color.green)
}
txtbutton2.setOnClickListener {
Log.e("Clicked", "Exception on delivery")
txttvdone.setBackgroundResource(R.color.orange)
}
}

}

override fun getItemCount(): Int { return movieList.size + 1 // one more to add header row
}

class RowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){


val txttvdone:TextView = itemView.findViewById<TextView>(R.id.txttvdone)
val txtbutton1:Button = itemView.findViewById<Button>(R.id.txtbutton1)
val txtbutton2:Button = itemView.findViewById<Button>(R.id.txtbutton2)
}

class MyViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
var txtbutton1 = view.findViewById<Button>(R.id.txtbutton1)
val txtbutton2:Button = itemView.findViewById<Button>(R.id.txtbutton2)
var txttvdone = view.findViewById<TextView>(R.id.txttvdone)
}
}


Related Topics



Leave a reply



Submit