ac4android
Well-Known Member
I am trying to implement a stop-watch on Android.
I can build the "gradle" based on Nexus4 API21, but when I click on the "Start" button, I keep getting this error, which doesn't make much sense to me.
01-05 14:37:17.961 1131-1131/com.hfad.stopwatch E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hfad.stopwatch, PID: 1131
java.lang.IllegalStateException: Could not find a method onClickStart(View) in the activity class com.hfad.stopwatch.StopwatchActivity for onClick handler on view class android.widget.Button with id 'start_button'
...
Here is the "content_stopwatch.xml", invoked by "activity_stopwatch.xml", as you can see I have the "@+id/start_button"
<TextView
android:id="@+id/time_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="92sp" />
<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/time_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android
nClick="onClickStart"
android:text="@String/start" />
The values in string.xml
<resources>
<string name="app_name">Stopwatch</string>
<string name="action_settings">Settings</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="reset">Reset</string>
</resources>
Here is the Java code snippet :
Can anyone see what I have missed?
I can build the "gradle" based on Nexus4 API21, but when I click on the "Start" button, I keep getting this error, which doesn't make much sense to me.
01-05 14:37:17.961 1131-1131/com.hfad.stopwatch E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hfad.stopwatch, PID: 1131
java.lang.IllegalStateException: Could not find a method onClickStart(View) in the activity class com.hfad.stopwatch.StopwatchActivity for onClick handler on view class android.widget.Button with id 'start_button'
...
Here is the "content_stopwatch.xml", invoked by "activity_stopwatch.xml", as you can see I have the "@+id/start_button"
<TextView
android:id="@+id/time_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="92sp" />
<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/time_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android
nClick="onClickStart"android:text="@String/start" />
The values in string.xml
<resources>
<string name="app_name">Stopwatch</string>
<string name="action_settings">Settings</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="reset">Reset</string>
</resources>
Here is the Java code snippet :
Code:
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.app.Activity;
import android.widget.TextView;
public class StopwatchActivity extends Activity {
// number of seconds displayed on the stopwatch.
private int seconds = 0;
// is stop watch running?
private boolean running;
private boolean wasRunning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
if (savedInstanceState != null) {
seconds = savedInstanceState.getInt("seconds");
running = savedInstanceState.getBoolean("running");
wasRunning = savedInstanceState.getBoolean("wasRunning");
}
runTimer();
}
// many lines elided...
@Override
protected void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putInt("seconds", seconds);
savedInstanceState.putBoolean("running", running);
savedInstanceState.putBoolean("wasRunning", wasRunning);
}
// start the stopwatch running when the Start button is clicked.
protected void onClickStart(View view){
running = true;
}
Can anyone see what I have missed?