I've tried to indicate when the user releases
the long click
by this -- answer --
Java code:
package com.time.reactiontime;
import com.time.reactiontime.R;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.media.*;
import java.io.*;
import java.util.*;
public class MainActivity extends Activity {
private int time;
private int militime;
private int Timecounter = 0;
private TextView textview;
private Button button1;
private boolean isSpeakButtonLongPressed = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
textview = ((TextView) findViewById(R.id.textview));
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnLongClickListener(speakHoldListener);
button1.setOnTouchListener(speakTouchListener);
}
private View.OnLongClickListener speakHoldListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View pView) {
final Random rand = new Random();
time = rand.nextInt(15) + 1;
militime = time * 1000;
new CountDownTimer(militime, 1000) {
public void onTick(long millisUntilFinished) {
button1.setText("Wait for sound and release");
}
public void onFinish() {
SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC,
0);
int soundId = sp.load(getBaseContext(),
R.raw.windows_8_notify, 1);
sp.play(soundId, 1, 1, 0, 0, 1);
MediaPlayer mPlayer = MediaPlayer.create(getBaseContext(),
R.raw.windows_8_notify);
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
} catch (IOException e) {
}
mPlayer.start();
Timer t = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Timecounter++;
}
});
}
};
t.scheduleAtFixedRate(task, 0, 1000);
}
}.start();
isSpeakButtonLongPressed = true;
return true;
}
};
private View.OnTouchListener speakTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View pView, MotionEvent pEvent) {
pView.onTouchEvent(pEvent);
if (pEvent.getAction() == MotionEvent.ACTION_UP) {
if (isSpeakButtonLongPressed) {
textview.setText("" + Timecounter);
isSpeakButtonLongPressed = false;
}
}
return false;
}
};
}
What I have tried to do is a little game of reaction time, when you long press it, after random number of seconds, it will make a sound and you have to release the button when you hear the sound, when the sound is being created, it also creates a timer and when you release the button it says to you how much time it took to you to hear the sound and release the button.
my problem is, the code is not working, I get errors in the logcat:
08-13 16:05:01.728: E/AndroidRuntime(1173): FATAL EXCEPTION: main
08-13 16:05:01.728: E/AndroidRuntime(1173): Process: com.time.reactiontime, PID: 1173
08-13 16:05:01.728: E/AndroidRuntime(1173): java.lang.NullPointerException
08-13 16:05:01.728: E/AndroidRuntime(1173): at com.time.reactiontime.MainActivity$1$1.onTick(MainActivity.java:42)
08-13 16:05:01.728: E/AndroidRuntime(1173): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
08-13 16:05:01.728: E/AndroidRuntime(1173): at android.os.Handler.dispatchMessage(Handler.java:102)
08-13 16:05:01.728: E/AndroidRuntime(1173): at android.os.Looper.loop(Looper.java:136)
08-13 16:05:01.728: E/AndroidRuntime(1173): at android.app.ActivityThread.main(ActivityThread.java:5118)
08-13 16:05:01.728: E/AndroidRuntime(1173): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 16:05:01.728: E/AndroidRuntime(1173): at java.lang.reflect.Method.invoke(Method.java:515)
08-13 16:05:01.728: E/AndroidRuntime(1173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
08-13 16:05:01.728: E/AndroidRuntime(1173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
08-13 16:05:01.728: E/AndroidRuntime(1173): at dalvik.system.NativeStart.main(Native Method)
The error is in this line `button1.setText("Wait for sound and release"); but when I try to delete this line it gives an other error..
How can I fix it ?