Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

App crashes/ implementing picture in picture mode to timer

+0
−2

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

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>
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Application logs (2 comments)

0 answers

Sign up to answer this question »