Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
When I tried to emulate the app that I coded in the connected phone, it keeps closing due to crashes, is there any problem with my code? Also, I am trying to make a timer app with picture and pict...
#2: Post edited
When I tried to emmulate the app that I coded in the connected phone, it keeps closing due to crashes, is there any problem in my code?Also, I am trying to make a timer app with picture and picture mode. I followed the tutorials and try to to combine these:- https://www.youtube.com/watch?v=AIhBJIdP6HE
- https://www.youtube.com/watch?v=lDpd3mLWYK4&feature=emb_title
It works individually but when I tried to combine, it does not work. Could I please get help for it? Thank youMainactivity`class MainActivity : AppCompatActivity()- {
- lateinit var binding: ActivityMainBinding
- lateinit var dataHelper: DataHelper
- private val timer = Timer()
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- PIP.setOnClickListener {
- if (Build.VERSION.SDK_INT >= VERSION_CODES.O) {
- val mpipParams = PictureInPictureParams.Builder()
- val display = windowManager.defaultDisplay
- val point = Point()
- display.getSize(point)
- mpipParams.setAspectRatio(Rational(point.x,point.y))
- enterPictureInPictureMode(mpipParams.build())
- }
- }
- binding = ActivityMainBinding.inflate(layoutInflater)
- setContentView(binding.root)
- dataHelper = DataHelper(applicationContext)
- binding.startButton.setOnClickListener { startStopAction() }
- binding.resetButton.setOnClickListener { resetAction() }
- if(dataHelper.timerCounting())
- {
- startTimer()
- }
- else
- {
- stopTimer()
- if(dataHelper.startTime() !=null&& dataHelper.stopTime() != null)
- {
- val time= Date().time - calcRestartTime().time
- binding.timeTv.text = timeStringFromDoubleLong(time)
- }
- }
- timer.scheduleAtFixedRate(TimeTask(), 0, 500)
- }
- override fun onPictureInPictureModeChanged(
- isInPictureInPictureMode: Boolean,
- newConfig: Configuration?
- ) {
- if (isInPictureInPictureMode){
- PIP.visibility = View.GONE
- timeTv.visibility = View.VISIBLE
- startButton. visibility = View.GONE
- resetButton.visibility = View.GONE
- }
- }
- private inner class TimeTask: TimerTask()
- {
- override fun run() {
- if (dataHelper.timerCounting())
- {
- val time = Date().time - dataHelper.startTime()!!.time
- binding.timeTv.text = timeStringFromDoubleLong(time)
- }
- }
- }
- private fun resetAction() {
- dataHelper.setStopTime(null)
- dataHelper.setStartTime(null)
- stopTimer()
- binding.timeTv.text = timeStringFromDoubleLong(0)
- }
- private fun stopTimer() {
- dataHelper.setTimerCounting(false)
- binding.startButton.text = getString(R.string.start)
- }
- private fun startTimer() {
- dataHelper.setTimerCounting(true)
- binding.startButton.text = getString(R.string.stop)
- }
- private fun startStopAction() {
- if (dataHelper.timerCounting())
- {
- dataHelper.setStopTime(Date())
- stopTimer()
- }
- else{
- if(dataHelper.stopTime() !=null)
- {
- dataHelper.setStartTime(calcRestartTime())
- dataHelper.setStopTime(null)
- }
- else{
- dataHelper.setStartTime(Date())
- }
- startTimer()
- }
- }
- private fun calcRestartTime(): Date
- {
- val diff = dataHelper.startTime()!!.time - dataHelper.stopTime()!!.time
- return Date(System.currentTimeMillis() + diff)
- }
- private fun timeStringFromDoubleLong(ms: Long):String
- {
- val seconds = (ms / 1000) % 60
- val minutes = ((ms / 1000 * 60) % 60)
- val hours = ((ms / 1000 * 60 *60) % 24)
- return makeTimeString(hours, minutes, seconds)
- }
- private fun makeTimeString(hours: Long, minutes: Long, seconds: Long): String
- {
- return String.format("%02d:%02d:%02d", hours, minutes, seconds)
- }
- }
`.xml- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity"
- android:background="#88DEFB">
- <Button
- android:id="@+id/PIP"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="PIP"
- android:textSize="30sp" />
- <TextView
- android:id="@+id/timeTv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:padding="10dp"
- android:text="@string/place_holder_time"
- android:textAlignment="center"
- android:textColor="@color/black"
- android:textSize="60sp" />
- <androidx.constraintlayout.widget.ConstraintLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:padding="10dp"
- >
- <Button
- android:id="@+id/startButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="72dp"
- android:layout_marginLeft="72dp"
- android:text="@string/start"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/resetButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/reset"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/startButton"
- app:layout_constraintTop_toTopOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- </LinearLayout>
- When I tried to emulate the app that I coded in the connected phone, it keeps closing due to crashes, is there any problem with my code?
- Also, I am trying to make a timer app with picture and picture mode. I followed the tutorials and try to combine these:
- https://www.youtube.com/watch?v=AIhBJIdP6HE
- https://www.youtube.com/watch?v=lDpd3mLWYK4&feature=emb_title
- It works individually but when I tried to combine them, it does not work.
- ### Mainactivity
- ```java
- class MainActivity : AppCompatActivity()
- {
- lateinit var binding: ActivityMainBinding
- lateinit var dataHelper: DataHelper
- private val timer = Timer()
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- PIP.setOnClickListener {
- if (Build.VERSION.SDK_INT >= VERSION_CODES.O) {
- val mpipParams = PictureInPictureParams.Builder()
- val display = windowManager.defaultDisplay
- val point = Point()
- display.getSize(point)
- mpipParams.setAspectRatio(Rational(point.x,point.y))
- enterPictureInPictureMode(mpipParams.build())
- }
- }
- binding = ActivityMainBinding.inflate(layoutInflater)
- setContentView(binding.root)
- dataHelper = DataHelper(applicationContext)
- binding.startButton.setOnClickListener { startStopAction() }
- binding.resetButton.setOnClickListener { resetAction() }
- if(dataHelper.timerCounting())
- {
- startTimer()
- }
- else
- {
- stopTimer()
- if(dataHelper.startTime() !=null&& dataHelper.stopTime() != null)
- {
- val time= Date().time - calcRestartTime().time
- binding.timeTv.text = timeStringFromDoubleLong(time)
- }
- }
- timer.scheduleAtFixedRate(TimeTask(), 0, 500)
- }
- override fun onPictureInPictureModeChanged(
- isInPictureInPictureMode: Boolean,
- newConfig: Configuration?
- ) {
- if (isInPictureInPictureMode){
- PIP.visibility = View.GONE
- timeTv.visibility = View.VISIBLE
- startButton. visibility = View.GONE
- resetButton.visibility = View.GONE
- }
- }
- private inner class TimeTask: TimerTask()
- {
- override fun run() {
- if (dataHelper.timerCounting())
- {
- val time = Date().time - dataHelper.startTime()!!.time
- binding.timeTv.text = timeStringFromDoubleLong(time)
- }
- }
- }
- private fun resetAction() {
- dataHelper.setStopTime(null)
- dataHelper.setStartTime(null)
- stopTimer()
- binding.timeTv.text = timeStringFromDoubleLong(0)
- }
- private fun stopTimer() {
- dataHelper.setTimerCounting(false)
- binding.startButton.text = getString(R.string.start)
- }
- private fun startTimer() {
- dataHelper.setTimerCounting(true)
- binding.startButton.text = getString(R.string.stop)
- }
- private fun startStopAction() {
- if (dataHelper.timerCounting())
- {
- dataHelper.setStopTime(Date())
- stopTimer()
- }
- else{
- if(dataHelper.stopTime() !=null)
- {
- dataHelper.setStartTime(calcRestartTime())
- dataHelper.setStopTime(null)
- }
- else{
- dataHelper.setStartTime(Date())
- }
- startTimer()
- }
- }
- private fun calcRestartTime(): Date
- {
- val diff = dataHelper.startTime()!!.time - dataHelper.stopTime()!!.time
- return Date(System.currentTimeMillis() + diff)
- }
- private fun timeStringFromDoubleLong(ms: Long):String
- {
- val seconds = (ms / 1000) % 60
- val minutes = ((ms / 1000 * 60) % 60)
- val hours = ((ms / 1000 * 60 *60) % 24)
- return makeTimeString(hours, minutes, seconds)
- }
- private fun makeTimeString(hours: Long, minutes: Long, seconds: Long): String
- {
- return String.format("%02d:%02d:%02d", hours, minutes, seconds)
- }
- }
- ```
- ### .xml
- ```xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity"
- android:background="#88DEFB">
- <Button
- android:id="@+id/PIP"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="PIP"
- android:textSize="30sp" />
- <TextView
- android:id="@+id/timeTv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:padding="10dp"
- android:text="@string/place_holder_time"
- android:textAlignment="center"
- android:textColor="@color/black"
- android:textSize="60sp" />
- <androidx.constraintlayout.widget.ConstraintLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:padding="10dp"
- >
- <Button
- android:id="@+id/startButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="72dp"
- android:layout_marginLeft="72dp"
- android:text="@string/start"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/resetButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/reset"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/startButton"
- app:layout_constraintTop_toTopOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
- </LinearLayout>
- ```
#1: Initial revision
App crashes/ implementing picture in picture mode to timer
When I tried to emmulate the app that I coded in the connected phone, it keeps closing due to crashes, is there any problem in my code? Also, I am trying to make a timer app with picture and picture mode. I followed the tutorials and try to to combine these: https://www.youtube.com/watch?v=AIhBJIdP6HE https://www.youtube.com/watch?v=lDpd3mLWYK4&feature=emb_title It works individually but when I tried to combine, it does not work. Could I please get help for it? Thank you Mainactivity `class MainActivity : AppCompatActivity() { lateinit var binding: ActivityMainBinding lateinit var dataHelper: DataHelper private val timer = Timer() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) PIP.setOnClickListener { if (Build.VERSION.SDK_INT >= VERSION_CODES.O) { val mpipParams = PictureInPictureParams.Builder() val display = windowManager.defaultDisplay val point = Point() display.getSize(point) mpipParams.setAspectRatio(Rational(point.x,point.y)) enterPictureInPictureMode(mpipParams.build()) } } binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) dataHelper = DataHelper(applicationContext) binding.startButton.setOnClickListener { startStopAction() } binding.resetButton.setOnClickListener { resetAction() } if(dataHelper.timerCounting()) { startTimer() } else { stopTimer() if(dataHelper.startTime() !=null&& dataHelper.stopTime() != null) { val time= Date().time - calcRestartTime().time binding.timeTv.text = timeStringFromDoubleLong(time) } } timer.scheduleAtFixedRate(TimeTask(), 0, 500) } override fun onPictureInPictureModeChanged( isInPictureInPictureMode: Boolean, newConfig: Configuration? ) { if (isInPictureInPictureMode){ PIP.visibility = View.GONE timeTv.visibility = View.VISIBLE startButton. visibility = View.GONE resetButton.visibility = View.GONE } } private inner class TimeTask: TimerTask() { override fun run() { if (dataHelper.timerCounting()) { val time = Date().time - dataHelper.startTime()!!.time binding.timeTv.text = timeStringFromDoubleLong(time) } } } private fun resetAction() { dataHelper.setStopTime(null) dataHelper.setStartTime(null) stopTimer() binding.timeTv.text = timeStringFromDoubleLong(0) } private fun stopTimer() { dataHelper.setTimerCounting(false) binding.startButton.text = getString(R.string.start) } private fun startTimer() { dataHelper.setTimerCounting(true) binding.startButton.text = getString(R.string.stop) } private fun startStopAction() { if (dataHelper.timerCounting()) { dataHelper.setStopTime(Date()) stopTimer() } else{ if(dataHelper.stopTime() !=null) { dataHelper.setStartTime(calcRestartTime()) dataHelper.setStopTime(null) } else{ dataHelper.setStartTime(Date()) } startTimer() } } private fun calcRestartTime(): Date { val diff = dataHelper.startTime()!!.time - dataHelper.stopTime()!!.time return Date(System.currentTimeMillis() + diff) } private fun timeStringFromDoubleLong(ms: Long):String { val seconds = (ms / 1000) % 60 val minutes = ((ms / 1000 * 60) % 60) val hours = ((ms / 1000 * 60 *60) % 24) return makeTimeString(hours, minutes, seconds) } private fun makeTimeString(hours: Long, minutes: Long, seconds: Long): String { return String.format("%02d:%02d:%02d", hours, minutes, seconds) } } ` .xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" android:background="#88DEFB"> <Button android:id="@+id/PIP" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="PIP" android:textSize="30sp" /> <TextView android:id="@+id/timeTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:padding="10dp" android:text="@string/place_holder_time" android:textAlignment="center" android:textColor="@color/black" android:textSize="60sp" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" > <Button android:id="@+id/startButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="72dp" android:layout_marginLeft="72dp" android:text="@string/start" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/resetButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reset" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/startButton" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </LinearLayout>