2016-05-23 166 views
0

我製作了一個應用程序,用於統計比賽中的得分目標(當您與朋友一起玩時,並且您懶得數數分數時:D)我想讓一個倒數計時器來計算比賽的時間。 當我按下開始按鈕啓動倒計時器時,我的應用程序崩潰。我有2個活動。主要活動是計算2個團隊的分數的代碼。Android Studio:開始倒數計時器時應用程序崩潰

這是我的第二個活動代碼(其中CountDowntimer應該的。):

package com.example.robert.scorecount; 

import android.os.CountDownTimer; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import java.util.concurrent.TimeUnit; 


public class SecondActivity extends AppCompatActivity { 

    Button StartButton,StopButton; 
    TextView TimerText; 

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

     Button StartButton = (Button) findViewById(R.id.StartButton); 
     Button StopButton = (Button) findViewById(R.id.StopButton); 
     TextView TimerText = (TextView) findViewById(R.id.Timer); 

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

    final CounterClass timer = new CounterClass(3600000,1000); 

     assert StartButton != null; 
     StartButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       timer.start(); 
      } 
     }); 


     assert StopButton != null; 
     StopButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       timer.cancel(); 
      } 
     }); 



    } 

    public class CounterClass extends CountDownTimer{ 

     public CounterClass(long millisInFuture, long countDownInterval) { 
      super(millisInFuture, countDownInterval); 
     } 



     @Override 
     public void onTick(long millisUntilFinished) { 
      String HourMinutesSeconds = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished), 
        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), 
        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))); 
      TimerText.setText(HourMinutesSeconds); 



     } 

     @Override 
     public void onFinish() { 
      TimerText.setText("Finished."); 


     } 
    } 





} 

,這是我的佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.robert.scorecount.SecondActivity"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Timer Settings" 
     android:id="@+id/textView" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:textSize="20dp" 
     android:textAlignment="center" /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Start" 
     android:id="@+id/StartButton" 
     android:layout_marginBottom="37dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="00:00" 
     android:id="@+id/Timer" 
     android:textSize="30dp" 
     android:layout_marginTop="88dp" 
     android:layout_below="@+id/ThirtyMinMatch" 
     android:layout_centerHorizontal="true" /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Stop" 
     android:id="@+id/StopButton" 
     android:layout_above="@+id/StartButton" 
     android:layout_centerHorizontal="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="30 Minutes" 
     android:id="@+id/ThirtyMinMatch" 
     android:layout_marginTop="24dp" 
     android:layout_below="@+id/textView" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_toLeftOf="@+id/Timer" 
     android:layout_toStartOf="@+id/Timer" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="1 Hour" 
     android:id="@+id/OneHourMinMatch" 
     android:layout_alignTop="@+id/ThirtyMinMatch" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_toRightOf="@+id/Timer" 
     android:layout_toEndOf="@+id/Timer" /> 

</RelativeLayout> 

回答

0

您的代碼:

TextView TimerText = (TextView) findViewById(R.id.Timer); 

改變這一行,只是刪除「TextView」,它已經在頂部。

TimerText = (TextView) findViewById(R.id.Timer); 
相關問題