2015-12-12 148 views
0

該應用程序用作倒數開始和停止按鈕。但我不知道什麼是錯...Android:不幸的是,應用程序已停止

我使用Android Studio 1.5.1與它的模擬器或我的智能手機,但在同樣的故障發生。

這是我的主要活動:

<?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="fill_parent" 
 
    android:layout_height="fill_parent" 
 
    tools:context="letscho.timer.MainActivity"> 
 

 

 
    <Button 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:id="@+id/btnStart" 
 
     android:layout_alignParentLeft="true" 
 
     android:layout_centerVertical="true" 
 
     android:layout_marginLeft="28dp" 
 
     android:text="Start"/> 
 

 
    <Button 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:id="@+id/btnStop" 
 
     android:layout_alignParentRight="true" 
 
     android:layout_centerVertical="true" 
 
     android:layout_marginRight="67dp" 
 
     android:text="Stop"/> 
 

 

 
    <TextView 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:id="@+id/textViewTime" 
 
     android:layout_above="@+id/btnStart" 
 
     android:layout_centerHorizontal="true" 
 
     android:layout_marginBottom="17dp" 
 
     android:textSize="25sp"/> 
 
</RelativeLayout>

我的MainActivity:

package letscho.timer; 
 

 
import android.annotation.SuppressLint; 
 
import android.annotation.TargetApi; 
 
import android.os.Build; 
 
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; 
 

 

 
@TargetApi(Build.VERSION_CODES.GINGERBREAD) 
 
@SuppressLint("NewApi") 
 
public class MainActivity extends AppCompatActivity { 
 

 
    Button btnStart, btnStop; 
 
    TextView textViewTime; 
 

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

 
     btnStart = (Button) findViewById(R.id.btnStart); 
 
     btnStart = (Button) findViewById(R.id.btnStop); 
 
     textViewTime = (TextView) findViewById(R.id.textViewTime); 
 

 
     textViewTime.setText("00:03:00"); 
 

 
     final CounterClass timer = new CounterClass(18000, 1000); 
 
     btnStart.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View v) { 
 
       timer.start(); 
 
      } 
 
     }); 
 

 
     btnStop.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View v) { 
 
       timer.cancel(); 
 
      } 
 
     }); 
 

 
    } 
 

 
    @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
 
    @SuppressLint("NewApi") 
 
    public class CounterClass extends CountDownTimer { 
 
     /** 
 
     * @param millisInFuture The number of millis in the future from the call 
 
     *       to {@link #start()} until the countdown is done and {@link #onFinish()} 
 
     *       is called. 
 
     * @param countDownInterval The interval along the way to receive 
 
     *       {@link #onTick(long)} callbacks. 
 
     */ 
 
     public CounterClass(long millisInFuture, long countDownInterval) { 
 
      super(millisInFuture, countDownInterval); 
 
     } 
 

 
     @SuppressLint("NewApi") 
 
     @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
 
     @Override 
 
     public void onTick(long millisUntilFinished) { 
 

 
      long millis = millisUntilFinished; 
 
      String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), 
 
        TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), 
 
        TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); 
 
      System.out.println(hms); 
 
      textViewTime.setText(hms); 
 

 
     } 
 

 
     @Override 
 
     public void onFinish() { 
 
      textViewTime.setText("00:00:00"); 
 

 
     } 
 
    } 
 

 
}

+1

你可以發佈日誌嗎? –

+0

發佈logcat錯誤消息。 – ridoy

+3

https://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this – CommonsWare

回答

4

以及您的問題是

btnStart = (Button) findViewById(R.id.btnStart); 
    btnStart = (Button) findViewById(R.id.btnStop); 

兩者引用相同的位置,然後你在一個空refrenece設置ClickListener 。

btnStop.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      timer.cancel(); 
     } 
    }); 

btnStop爲空。

相關問題