BallisticMonkey
BallisticMonkey

Reputation: 83

Android App Crashes with while loop

I'm making this app that tells how much longer till we reach a destination but whenever I try to use it it just black screens just crashes. I think it has something to do with the loop but I can't find anything online Can anyone help? Thanks in advance.................................................................................................................

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.Calendar;
import java.util.TimeZone;
import java.time.*;
import java.math.*;

public class TimeTIll extends AppCompatActivity {
    private static final String DateUtils = null;
    TextView txt;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_till);
        txt = (TextView) findViewById(R.id.TimeDipslay);
        int i = 0;
        while (i < 10) {
            Program();
            i++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
    }

    public void Program() {

        int totalMinutesSinceMidnight = 0;
        long ms = 0;
        long secondsM = 0;
        long SecondsTillEstimate = 0;
        long SecondsTillArrival1 = 0;
        double MinutesTillArrival = 0;
        double hoursTillArrival = 0;
        double SecondsTillArrival = 0;
        Calendar now = Calendar.getInstance();
        Calendar midnight = Calendar.getInstance();
        midnight.set(Calendar.HOUR_OF_DAY, 0);
        midnight.set(Calendar.MINUTE, 0);
        midnight.set(Calendar.SECOND, 0);
        midnight.set(Calendar.MILLISECOND, 0);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }


        now = Calendar.getInstance();
        midnight = Calendar.getInstance();

        ms = now.getTime().getTime() - midnight.getTime().getTime();
        totalMinutesSinceMidnight = (int) (ms / 1000);
        secondsM = totalMinutesSinceMidnight;

        SecondsTillEstimate = (60 * 60 * 23);
        SecondsTillArrival1 = SecondsTillEstimate - secondsM;
        hoursTillArrival = Math.floor(SecondsTillArrival1 / 3600);
        SecondsTillArrival1 -= (hoursTillArrival * 3600);
        MinutesTillArrival = Math.floor(SecondsTillArrival1 / 60);
        SecondsTillArrival1 -= (MinutesTillArrival * 60);
        SecondsTillArrival = SecondsTillArrival1;
        txt.setText((int) hoursTillArrival + " Hours " + (int) MinutesTillArrival + " Minutes " + (int) SecondsTillArrival + " Seconds Till Arrival In Floriada");

    }


}

Upvotes: 0

Views: 1009

Answers (3)

Mark S. Khalil
Mark S. Khalil

Reputation: 157

You Shouldn`t use it inside main thread specially when you are doing operation inside it. You may have to use some thing like that.

   viewModelScope.launch(Dispatchers.IO) {
        while (statues==true){
            if (statue==true){
                delay(500)
                   //ToDo


             }
        }
    }

Upvotes: 0

falberto89
falberto89

Reputation: 153

I think that the problem is that you are trying to make sleep the main thread of the application. In Android I knew that for security reasons this can't be done. (Look this https://www.codementor.io/tips/0743378261/non-freezing-sleep-in-android-app for details )

Try this solution instead:

How to pause / sleep thread or process in Android?

Upvotes: 0

Tyler V
Tyler V

Reputation: 10910

Please read about the activity life-cycle. You cannot call long-running tasks in onCreate and expect them to show anything other than an ANR crash (your onCreate method blocks the UI thread with all those sleep statements and generates an ANR crash before it even gets to the onStart and onResume stages where it actually shows the activity).

If you want the UI to change dynamically, you could use a handler to execute a method at a regular interval. For example, to update something at a regular interval (1000 ms here) you could do:

public class MainActivity extends AppCompatActivity {

    private final Handler handler = new Handler();
    private TextView txt;
    private int count = 0;
    private final int updateFreqMs = 1000; // call update every 1000 ms

    @Override
    protected void onPause() {
        super.onPause();
        handler.removeCallbacksAndMessages(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                updateTime();
                handler.postDelayed(this, updateFreqMs);
            }
        }, updateFreqMs);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_till);
        txt = findViewById(R.id.TimeDipslay);
    }

    private void updateTime() {
        // here you update the displayed value
        // this will be called every second indefinitely
        // do your math and generate a string to print, just showing
        // a counter here to demonstrate
        ++count;
        txt.setText("Count = " + count);
    }
}

Upvotes: 1

Related Questions