-1

I have set up a stopwatch app that will start counting up when i hit the button. But when i click the button the emulator crashes and makes me force close. I have tested the actual code with System.out.print() and the code portion works; But the emulator crashes :/ Any help would be appreciated!

MainActivity:

package com.example.stopwatch;
package com.example.stopwatch;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private ClockCounter mClockCounter = new ClockCounter();
    boolean ifPressed = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Set Views
        final TextView currentTime = (TextView) findViewById(R.id.textView1);
        Button startStopButton = (Button) findViewById(R.id.button1);

        startStopButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //turns stopwatch on and off.
                ifPressed ^= true;

                //runs the loop
                try{
                    while(ifPressed){//this will only run if ifPressed is true

                          Thread.sleep(1000);//makes the loop wait 1 second

                          String time = mClockCounter.getTime();
                            //Set time to TextView

                            currentTime.setText(time);
                        }
                }catch(InterruptedException ex){
                    //idk lmao
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

The ClockCounter method is just the counter and returns the time as a String.

Activity_Main.xml(sorry if this is irrelevant):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:weightSum="1" >

        <View
            android:id="@+id/view1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.2" />

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:text="@string/StartStop" />

        <View
            android:id="@+id/View2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/linearLayout1"
        android:orientation="vertical"
        android:weightSum="1" >

        <View
            android:id="@+id/view3"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.1" />

        <AnalogClock
            android:id="@+id/analogClock1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.8" />

        <DigitalClock
            android:id="@+id/digitalClock1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:text="DigitalClock" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:weightSum="1" >

        <View
            android:id="@+id/view4"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.3" />

        <LinearLayout
            android:id="@+id/LinearLayout01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1" >

            <View
                android:id="@+id/View01"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0.2" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:layout_weight="0.60"
                android:text="00:00:00"
                android:textSize="50dp"
                android:width="@dimen/TimerSize" />

            <View
                android:id="@+id/View02"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0.2" />

        </LinearLayout>

        <View
            android:id="@+id/view5"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.3" />

    </LinearLayout>

</RelativeLayout>

Console:

[2013-12-30 00:24:31 - StopWatch] ------------------------------
[2013-12-30 00:24:31 - StopWatch] Android Launch!
[2013-12-30 00:24:31 - StopWatch] adb is running normally.
[2013-12-30 00:24:31 - StopWatch] Performing com.example.stopwatch.MainActivity activity launch
[2013-12-30 00:24:31 - StopWatch] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Android41'
[2013-12-30 00:24:33 - StopWatch] Application already deployed. No need to reinstall.
[2013-12-30 00:24:33 - StopWatch] Starting activity com.example.stopwatch.MainActivity on device emulator-5554
[2013-12-30 00:24:35 - StopWatch] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.stopwatch/.MainActivity }
[2013-12-30 00:24:35 - StopWatch] ActivityManager: Warning: Activity not started, its current task has been brought to the front

LogCat:

12-30 00:24:28.983: D/AndroidRuntime(732): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
12-30 00:24:28.983: D/AndroidRuntime(732): CheckJNI is ON
12-30 00:24:29.043: D/dalvikvm(732): Trying to load lib libjavacore.so 0x0
12-30 00:24:29.053: D/dalvikvm(732): Added shared lib libjavacore.so 0x0
12-30 00:24:29.114: D/dalvikvm(732): Trying to load lib libnativehelper.so 0x0
12-30 00:24:29.114: D/dalvikvm(732): Added shared lib libnativehelper.so 0x0
12-30 00:24:30.283: D/AndroidRuntime(732): Calling main entry com.android.commands.pm.Pm
12-30 00:24:30.343: D/AndroidRuntime(732): Shutting down VM
12-30 00:24:30.363: D/dalvikvm(732): GC_CONCURRENT freed 102K, 78% free 466K/2048K, paused 11ms+2ms, total 23ms
12-30 00:24:30.373: D/dalvikvm(732): Debugger has detached; object registry had 1 entries
12-30 00:24:31.094: D/AndroidRuntime(745): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
12-30 00:24:31.094: D/AndroidRuntime(745): CheckJNI is ON
12-30 00:24:31.145: D/dalvikvm(745): Trying to load lib libjavacore.so 0x0
12-30 00:24:31.154: D/dalvikvm(745): Added shared lib libjavacore.so 0x0
12-30 00:24:31.195: D/dalvikvm(745): Trying to load lib libnativehelper.so 0x0
12-30 00:24:31.195: D/dalvikvm(745): Added shared lib libnativehelper.so 0x0
12-30 00:24:32.284: D/AndroidRuntime(745): Calling main entry com.android.commands.am.Am
12-30 00:24:32.335: I/ActivityManager(161): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.stopwatch/.MainActivity u=0} from pid 745
12-30 00:24:32.354: D/AndroidRuntime(745): Shutting down VM
12-30 00:24:32.374: D/dalvikvm(745): GC_CONCURRENT freed 102K, 77% free 489K/2048K, paused 1ms+3ms, total 20ms
12-30 00:24:32.374: D/dalvikvm(745): Debugger has detached; object registry had 1 entries
12-30 00:25:36.473: D/dalvikvm(246): GC_CONCURRENT freed 384K, 7% free 8538K/9159K, paused 19ms+8ms, total 69ms
12-30 00:26:00.256: W/BroadcastQueue(161): Timeout of broadcast BroadcastRecord{41518378 android.intent.action.TIME_TICK} - receiver=android.os.BinderProxy@415928c8, started 60044ms ago
12-30 00:26:00.256: W/BroadcastQueue(161): Receiver during timeout: BroadcastFilter{415a2238 ReceiverList{415a7b40 716 com.example.stopwatch/10046 remote:415928c8}}
12-30 00:26:00.454: I/Process(161): Sending signal. PID: 716 SIG: 3
12-30 00:26:00.454: I/dalvikvm(716): threadid=3: reacting to signal 3
12-30 00:26:00.573: I/dalvikvm(716): Wrote stack traces to '/data/anr/traces.txt'
12-30 00:26:00.573: I/Process(161): Sending signal. PID: 161 SIG: 3
12-30 00:26:00.573: I/dalvikvm(161): threadid=3: reacting to signal 3
12-30 00:26:01.555: I/dalvikvm(161): Wrote stack traces to '/data/anr/traces.txt'
12-30 00:26:01.555: I/Process(161): Sending signal. PID: 216 SIG: 3
12-30 00:26:01.555: I/dalvikvm(216): threadid=3: reacting to signal 3
12-30 00:26:01.724: I/dalvikvm(216): Wrote stack traces to '/data/anr/traces.txt'
12-30 00:26:01.734: I/Process(161): Sending signal. PID: 246 SIG: 3
12-30 00:26:01.734: I/dalvikvm(246): threadid=3: reacting to signal 3
12-30 00:26:02.053: I/dalvikvm(246): Wrote stack traces to '/data/anr/traces.txt'
12-30 00:26:02.907: D/dalvikvm(161): GC_CONCURRENT freed 627K, 12% free 11626K/13191K, paused 77ms+15ms, total 344ms
12-30 00:26:02.907: D/dalvikvm(161): WAIT_FOR_CONCURRENT_GC blocked 74ms
12-30 00:26:02.914: D/dalvikvm(161): WAIT_FOR_CONCURRENT_GC blocked 0ms
12-30 00:26:03.124: D/dalvikvm(161): GC_EXPLICIT freed 121K, 13% free 11509K/13191K, paused 27ms+13ms, total 212ms
12-30 00:26:03.844: E/ActivityManager(161): ANR in com.example.stopwatch
12-30 00:26:03.844: E/ActivityManager(161): Reason: Broadcast of Intent { act=android.intent.action.TIME_TICK flg=0x40000014 (has extras) }
12-30 00:26:03.844: E/ActivityManager(161): Load: 0.32 / 0.4 / 0.35
12-30 00:26:03.844: E/ActivityManager(161): CPU usage from 55285ms to 0ms ago:
12-30 00:26:03.844: E/ActivityManager(161):   0.8% 161/system_server: 0.3% user + 0.4% kernel / faults: 15 minor
12-30 00:26:03.844: E/ActivityManager(161):   0.6% 246/com.android.phone: 0.3% user + 0.2% kernel / faults: 50 minor
12-30 00:26:03.844: E/ActivityManager(161):   0.4% 716/com.example.stopwatch: 0.3% user + 0.1% kernel / faults: 56 minor
12-30 00:26:03.844: E/ActivityManager(161):   0% 216/com.android.systemui: 0% user + 0% kernel / faults: 8 minor
12-30 00:26:03.844: E/ActivityManager(161):   0% 12/pdflush: 0% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161):   0% 34/rild: 0% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161):   0% 45/adbd: 0% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161): 2.4% TOTAL: 1.2% user + 1.1% kernel
12-30 00:26:03.844: E/ActivityManager(161): CPU usage from 2789ms to 3411ms later:
12-30 00:26:03.844: E/ActivityManager(161):   14% 161/system_server: 7.9% user + 6.3% kernel
12-30 00:26:03.844: E/ActivityManager(161):     17% 176/ActivityManager: 7.9% user + 9.5% kernel
12-30 00:26:03.844: E/ActivityManager(161):     1.5% 175/er.ServerThread: 1.5% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161):   1.3% 246/com.android.phone: 1.3% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161):     1.3% 499/GsmDC-1: 1.3% user + 0% kernel
12-30 00:26:03.844: E/ActivityManager(161): 60% TOTAL: 40% user + 20% kernel
12-30 00:26:04.184: I/Choreographer(161): Skipped 31 frames!  The application may be doing too much work on its main thread.
12-30 00:26:04.664: D/dalvikvm(161): GC_CONCURRENT freed 345K, 10% free 11997K/13191K, paused 75ms+77ms, total 761ms
12-30 00:26:04.674: D/dalvikvm(161): WAIT_FOR_CONCURRENT_GC blocked 673ms
12-30 00:26:04.844: D/dalvikvm(161): GC_FOR_ALLOC freed 439K, 12% free 11716K/13191K, paused 163ms, total 164ms

Thank You to anyone who can help!

1 Answers1

2

You get ANR (Application Not Responding) because your application is not responsive.

Do not run Thread.sleep() on the main thread as it's surely making your application not responsive, especially if you do it in a continous loop. You will need to redesign your code. If you need to do some sort of a timer, you should do it without blocking the main thread.

There's plenty of resources on that topic. Check this answer, for example: Android - Run a thread repeatingly within a timer

Community
  • 1
  • 1
Szymon
  • 42,577
  • 16
  • 96
  • 114