-1

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 ?

Community
  • 1
  • 1
Gadab
  • 13
  • 3
  • 3
    Do you know what a `NullPointerException` is? Randomly deleting lines isn't very methodical. Understand your problem. – keyser Aug 13 '14 at 13:07
  • deleting a line causing an issue isnt "random". debugging by breaking things more is my favorite method. – r2DoesInc Aug 13 '14 at 13:11
  • brother always try to understand your problem better way is to use debugger if still you are not able to find solution then ask it on stackoverflow – Android is everything for me Aug 13 '14 at 13:13
  • @keyser Actually I don't know what is it :( – Gadab Aug 13 '14 at 13:15
  • @r2DoesInc I just deleted it to try to understand what is the problem – Gadab Aug 13 '14 at 13:15
  • @Androidiseverythingforme What do you mean by use a debugger? – Gadab Aug 13 '14 at 13:16
  • debugger is tool in IDE means you can actually run time check the output step by step so you can analyse where your actually facing the problem by setting debug point in your code you need to clear your basic first brother else you will not go longer in development first learn basics from android site – Android is everything for me Aug 13 '14 at 13:19
  • Then start by searching for information on `NullPointerException`. Null is a fundamental programming concept, and quite easy to grasp once you get started. – keyser Aug 13 '14 at 13:20

2 Answers2

3

first thing you declare two Button object one global and one locally as final so please delete local one use global one

copy paste my below code it will surely work .

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));
        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;
        }
    };
}
1
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);

    }

You're ignoring your global button1 in favor of a local one.

Use this as your oncreate

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_main);
            textview = ((TextView) findViewById(R.id.textview));
            button1 = (Button) findViewById(R.id.button1);
            button1.setOnLongClickListener(speakHoldListener);
            button1.setOnTouchListener(speakTouchListener);

        }
keyser
  • 18,829
  • 16
  • 59
  • 101
r2DoesInc
  • 3,759
  • 3
  • 29
  • 60