0

I programmed a stopwatch but it works with java, but not on Android. If I press the Button it makes nothing. Could it be, that maybe Eclipse is wrong installed?

package com.example.stoppuhr;

import java.text.SimpleDateFormat;

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


public class MainActivity extends Activity implements OnClickListener {

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

    long start;
    long millis;

    public void onClick(View v) {
        Button start1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        TextView textView1 = (TextView)findViewById(R.id.textView1);
        start1.setOnClickListener(this);

I think here is the problem.

        if (v.getId() == start1.getId()){
            textView1.setText("moldu");
            start = System.currentTimeMillis();

            while (true){
                millis = System.currentTimeMillis()-start;



                SimpleDateFormat sdfDate = new SimpleDateFormat("mm:ss.SSS");//dd/MM/yyyy
                String ausgeben = sdfDate.format(millis);

                textView1.setText(ausgeben);
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }   
        }

    }

}

Thank you for your help

4

3 回答 3

3

Initialize your views in onCreate

Button start1,button2
TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    start1 = (Button)findViewById(R.id.button1);
    button2 = (Button)findViewById(R.id.button2);
    textView1 = (TextView)findViewById(R.id.textView1);
    start1.setOnClickListener(this); 
}

You are also calling Thread.sleep(30) on the ui thread. This will block the ui thread.

http://developer.android.com/training/articles/perf-anr.html

Quoting from the docs

If you implement Thread or HandlerThread, be sure that your UI thread does not block while waiting for the worker thread to complete—<strong>do not call Thread.wait() or Thread.sleep().

Make sure you do not call Thread.sleep() on the ui thread.

I suggest you have a look at the count down timer.

  1. Android Thread for a timer

  2. Countdowntimer in minutes and seconds

public static void sleep (long time) // in milliseconds

Added in API level 1 Causes the thread which sent this message to sleep for the given interval of time (given in milliseconds). The precision is not guaranteed - the Thread may sleep more or less than requested.

Parameters time The time to sleep in milliseconds. Throws InterruptedException if interrupt() was called for this Thread while it was sleeping See Also interrupt()

于 2013-08-20T11:58:38.563 回答
3
public void onClick(View v) {

Is a method that should be called when a button is clicked, but you have to register your class an OnClickListener for your button. You try to do this inside onClick.

So your class (MainActivity) will never be registered as an OnClickListener. Try moving this:

Button start1 = (Button)findViewById(R.id.button1);
start1.setOnClickListener(this);

to onCreate.

于 2013-08-20T11:59:08.910 回答
0

First thing you need to do: addOnClickListener(), you can read about it. I'd google it to understand how it works.

If you added this to your button, by using

Button buttonname = (Button) findViewById(R.id.yourbuttonid);
buttonname.setOnClickListener(this);

in your onCreate() method, you successfully added the onClickListener() to the button.
If this button is pressed now, the onClick() will be called.

于 2013-08-20T12:11:35.917 回答