我如何将秒表值传递给另一个活动

时间:2018-10-22 14:49:54

标签: android

我试图制作一个秒表来做这个测验,但我不知道如何将秒表从一个问题传递到另一个问题(每个活动1个问题)。秒表的代码如下。

SummaryStatistics<String> ss = IntStream.rangeClosed(0, s.length() - k)
    .mapToObj(value -> s.substring(value, value + k))
    .collect(SummaryStatistics.statistics(String.CASE_INSENSITIVE_ORDER));

String smallest = ss.getMin();
String largest  = ss.getMax();

已经尝试了一段时间,但是当我按下开始按钮并导致第一个问题时,应用程序仍然崩溃

我在每次活动开始时都准备好了这个,当出现新问题时要再次启动秒表,我认为这是崩溃的地方吗?

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

    timer = (TextView)findViewById(R.id.tvTimer);  
    start = (Button)findViewById(R.id.btStart);  
    pause = (Button)findViewById(R.id.btPause);  
    reset = (Button)findViewById(R.id.btReset);  

    handler = new Handler() ;  

    start.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View view) {  

            StartTime = SystemClock.uptimeMillis();  
            handler.postDelayed(runnable, 0);  

            reset.setEnabled(false);  

        }  
    });  

    pause.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View view) {  

            TimeBuff += MillisecondTime;  

            handler.removeCallbacks(runnable);  

            reset.setEnabled(true);  

        }  
    });  

    reset.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View view) {  

            MillisecondTime = 0L ;  
            StartTime = 0L ;  
            TimeBuff = 0L ;  
            UpdateTime = 0L ;  
            Seconds = 0 ;  
            Minutes = 0 ;  
            MilliSeconds = 0 ;  

            timer.setText("00:00:00");  

        }  
    });  

}  

public Runnable runnable = new Runnable() {  

    public void run() {  

        MillisecondTime = SystemClock.uptimeMillis() - StartTime;  

        UpdateTime = TimeBuff + MillisecondTime;  

        Seconds = (int) (UpdateTime / 1000);  

        Minutes = Seconds / 60;  

        Seconds = Seconds % 60;  

        MilliSeconds = (int) (UpdateTime % 1000);  

        timer.setText("" + Minutes + ":"  
                + String.format("%02d", Seconds) + ":"  
                + String.format("%03d", MilliSeconds));  

        handler.postDelayed(this, 0);  
    }  

};  

并在onCreate()中再次运行秒表

该行显示崩溃的行:

Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {

    @Override
    public void run() {
        long millis = System.currentTimeMillis() - startTime;
        int seconds = (int) (millis / 1000);
        int minutes = seconds / 60;
        seconds = seconds % 60;

        time = String.valueOf(millis);
        timerHandler.postDelayed(this, 500);
    }
};

日志:

long startTime = Long.parseLong(getIntent().getStringExtra("time"));

1 个答案:

答案 0 :(得分:0)

您可以:

  • 将秒表时间保存​​在全局/静态位置。
  • 传递并反对每个类。