设计秒表

时间:2014-06-11 10:56:37

标签: android

我创建了一个用作Stop Watch的应用程序,我在其中创建了多个选项卡,然后,在Stop Watch中,我使用System.currentTimeMillis()函数以毫秒为单位获取系统的时间。  一旦秒表停止,该功能可以正常显示时间,但与我们手机中内置的秒表不同,它显示瞬时值,当我尝试在应用程序中实现类似的东西时,通过使用开关获取按下按钮的id的情况,直到按下时间停止,我创建一个临时变量,并使用该临时变量使用相同的先前函数System.currentTimeMillis()获取当前时间的值。然后,计算秒,分和毫秒的值,我在TextView上写下我在布局中设置的值。但是,在这方面,程序会给出错误,而不使用瞬时值,程序运行良好。任何人都可以帮助我吗?

基本程序的代码添加在下面 -

case R.id.start:

    start1 = System.currentTimeMillis();
    //start.setBackgroundColor(Color.RED);
    //start.setTextColor(Color.WHITE);
    //start.setText("STOP");


    break;

    case R.id.stop:

    stop1 = System.currentTimeMillis();
    if(start1!=0){
        long ans = stop1 - start1;
        int milli = (int)(ans % 1000);
        int millis = (int)(milli/100);
        int seconds = (int)(ans/1000);
        int minutes = (seconds/60);

        result.setText(String.format("%d:%02d:%d", minutes, seconds, millis)); 
        start1 = 0;
        }

        break;

这是代码,工作正常但是当我尝试相同的停止时,在开始时,通过创建一个名为start2的临时变量,然后在所有时刻获取时间并显示它们,应用程序给出错误。下面添加了新的开始代码!

case R.id.start:
long stop1 = System.currentTimeMillis();
long ans = stop1 - start1;
int milli = (int)(ans % 1000);
int millis = (int)(milli/100);
int seconds = (int)(ans/1000);
int minutes = (seconds/60);
result.setText(String.format("%d:%02d:%d", minutes, seconds, millis)); 

该程序目前尚未正常运作。即使在按下停止按钮后,它也没有显示任何内容!

编辑:

所以,我现在正在使用的代码如下。原木猫在它下面!

case R.id.start:
    if(start.getText().toString().contentEquals("Start")){
    start1 = System.currentTimeMillis();
    start.setBackgroundColor(Color.RED);
    start.setTextColor(Color.WHITE);
    start.setText("Stop");
    stop1 = System.currentTimeMillis();
    long ans = stop1 - start1;
    int milli = (int)(ans % 1000);
    int millis = (int)(milli/100);
    int seconds = (int)(ans/1000);
    int minutes = (seconds/60);

    result.setText(String.format("%d:%02d:%d", minutes, seconds, millis));
    }
    else{
        start1 = System.currentTimeMillis();
        start.setBackgroundColor(Color.DKGRAY);
        start.setTextColor(Color.BLACK);
        start.setText("Start");
        stop1 = System.currentTimeMillis();
        long ans = stop1 - start1;
        int milli = (int)(ans % 1000);
        int millis = (int)(milli/100);
        int seconds = (int)(ans/1000);
        int minutes = (seconds/60);

        result.setText(String.format("%d:%02d:%d", minutes, seconds, millis));
    }


    break;

logCat如下所示:

06-11 07:34:26.580: E/AndroidRuntime(3070): FATAL EXCEPTION: main
06-11 07:34:26.580: E/AndroidRuntime(3070): Process: com.example.newapplication, PID: 3070
06-11 07:34:26.580: E/AndroidRuntime(3070): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newapplication/com.example.newapplication.Tabs}: java.lang.NullPointerException
06-11 07:34:26.580: E/AndroidRuntime(3070):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at android.os.Looper.loop(Looper.java:136)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at java.lang.reflect.Method.invoke(Method.java:515)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at dalvik.system.NativeStart.main(Native Method)
06-11 07:34:26.580: E/AndroidRuntime(3070): Caused by: java.lang.NullPointerException
06-11 07:34:26.580: E/AndroidRuntime(3070):     at com.example.newapplication.Tabs.onCreate(Tabs.java:36)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at android.app.Activity.performCreate(Activity.java:5231)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-11 07:34:26.580: E/AndroidRuntime(3070):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-11 07:34:26.580: E/AndroidRuntime(3070):     ... 11 more

编辑: 我正在尝试的新代码是通过实现一个Thread,然后等待10 ms,然后计算差异,似乎有一个错误,有线程实现,代码和logcat都附在下面!

case R.id.start:
    start1 = System.currentTimeMillis();

    Thread timer = new Thread(){
        public void run(){
            try{
            sleep(10);
            } catch(InterruptedException e){
                e.printStackTrace();
                    }   finally {
                    stop1 = System.currentTimeMillis();
                    long ans = stop1 - start1;
                    int milli = (int)(ans % 1000);
                    int millis = (int)(milli/100);
                    int seconds = (int)(ans/1000);
                    int minutes = (seconds/60);

                    result.setText(String.format("%d:%02d:%d", minutes, seconds, millis));

                }
            }
        };
        timer.start();

    break;
    case R.id.stop:
        stop1 = System.currentTimeMillis();
        long ans1 = stop1 - start1;
        int milli1 = (int)(ans1 % 1000);
        int millis1 = (int)(milli1/100);
        int seconds1 = (int)(ans1/1000);
        int minutes1 = (seconds1/60);

        result.setText(String.format("%d:%02d:%d", minutes1, seconds1, millis1));   

    case R.id.reset:
        result.setText("0:00:0");
        start1 = 0;
        break;

logcat的:

06-11 08:47:51.460: E/AndroidRuntime(3195): FATAL EXCEPTION: Thread-177
06-11 08:47:51.460: E/AndroidRuntime(3195): Process: com.example.newapplication, PID: 3195
06-11 08:47:51.460: E/AndroidRuntime(3195): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-11 08:47:51.460: E/AndroidRuntime(3195):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
06-11 08:47:51.460: E/AndroidRuntime(3195):     at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:857)
06-11 08:47:51.460: E/AndroidRuntime(3195):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)
06-11 08:47:51.460: E/AndroidRuntime(3195):     at android.view.View.invalidate(View.java:10935)
06-11 08:47:51.460: E/AndroidRuntime(3195):     at android.view.View.invalidate(View.java:10890)
06-11 08:47:51.460: E/AndroidRuntime(3195):     at android.widget.TextView.checkForRelayout(TextView.java:6579)
06-11 08:47:51.460: E/AndroidRuntime(3195):     at android.widget.TextView.setText(TextView.java:3813)
06-11 08:47:51.460: E/AndroidRuntime(3195):     at android.widget.TextView.setText(TextView.java:3671)
06-11 08:47:51.460: E/AndroidRuntime(3195):     at android.widget.TextView.setText(TextView.java:3646)
06-11 08:47:51.460: E/AndroidRuntime(3195):     at com.example.newapplication.Tabs$2.run(Tabs.java:98)

2 个答案:

答案 0 :(得分:0)

您必须在案例start1 = System.currentTimeMillis();的末尾使用start1 = 0代替R.id.start:。因为您在if(start1!=0)处进行了case R.id.stop:检查。这就是你的停止不起作用的原因。

答案 1 :(得分:0)

case R.id.start:中,您将时间保存在stop1变量long stop1 = System.currentTimeMillis();中,并将start1设置为0。

case R.id.stop: {if}条件if(start1!=0)检查变量start1之前设置为0。

if条件永远不会返回true,这就是为什么你在TextView

中看不到任何文字的原因

编辑:

在您的新实施中,您依次阅读start1stop1。两个变量都具有相同的值,因为在System.currentTimeMillis()的两次调用之间传递的少数指令执行时间不到1毫秒。因此ans将具有值0。

NullPointerException似乎是由其他内容引起的,我无法从您提供的代码段中找到答案。

case R.id.start:
    if(start.getText().toString().contentEquals("Start")){
    start1 = System.currentTimeMillis();
    start.setBackgroundColor(Color.RED);
    start.setTextColor(Color.WHITE);
    start.setText("Stop");
    stop1 = System.currentTimeMillis();
    long ans = stop1 - start1;
    int milli = (int)(ans % 1000);
    int millis = (int)(milli/100);
    int seconds = (int)(ans/1000);
    int minutes = (seconds/60);

    result.setText(String.format("%d:%02d:%d", minutes, seconds, millis));
    }
    else{
        start1 = System.currentTimeMillis();
        start.setBackgroundColor(Color.DKGRAY);
        start.setTextColor(Color.BLACK);
        start.setText("Start");
        stop1 = System.currentTimeMillis();
        long ans = stop1 - start1;
        int milli = (int)(ans % 1000);
        int millis = (int)(milli/100);
        int seconds = (int)(ans/1000);
        int minutes = (seconds/60);

        result.setText(String.format("%d:%02d:%d", minutes, seconds, millis));
    }


    break;