简体   繁体   English

在ACTION_DOWN触摸事件中使用Android Timer错误

[英]Android Timer error whenused in ACTION_DOWN touch event

I have been working on a very simple app. 我一直在开发一个非常简单的应用程序。 There is only one music file, one play button and nothing much. 只有一个音乐文件,一个播放按钮,没什么。 I am trying to make it so that the music starts playing when the button is clicked but then I'd like to have the user hold down the stop button for 2 seconds until it stops playing and in case the user releases the button within the 2 seconds, the timer should just terminate. 我正在尝试制作音乐,以便在单击按钮时开始播放音乐,但是我想让用户按住停止按钮2秒钟,直到停止播放为止,以防用户在2秒钟内松开按钮秒,计时器应终止。

import android.app.ActionBar;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

import java.util.Timer;
import java.util.TimerTask;

import static com.example.appname.R.raw.sound;


public class MainActivity extends ActionBarActivity {

    boolean isPlaying = false;
    ImageButton button;
    MediaPlayer mp;
    Timer timer;

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

        button = (ImageButton) findViewById(R.id.playButton);
        mp = MediaPlayer.create(this, sound);

        button.setOnClickListener(imgButtonHandler);


    }

    View.OnClickListener imgButtonHandler;

    {
        imgButtonHandler = new View.OnClickListener() {

            public void onClick(View v) {
                if (!isPlaying) {
                    mp.start();
                    isPlaying = true;
                    button.setImageResource(R.drawable.stop);
                    button.setOnTouchListener(imgButtonTouchHandler);
                }

            }
        };
    }

    View.OnTouchListener imgButtonTouchHandler;

    {
        imgButtonTouchHandler = new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // PRESSED
                        if (isPlaying) {
                            button.setImageResource(R.drawable.stop_highlighted);
                            timer.schedule(stopMusic(), 2000);

                            return true;
                        } else {
                            return false;
                        }

                    case MotionEvent.ACTION_UP:
                        if (isPlaying) {
                            if (timer != null){
                                timer.cancel();
                                timer = null;
                            }
                            return true;
                        } else {
                            return false;
                        }
                        // RELEASED
                }
                return false;
            }
        };
    }

    public TimerTask stopMusic() {
        mp.reset();
        mp = MediaPlayer.create(MainActivity.this, sound);
        button.setImageResource(R.drawable.play);
        isPlaying = false;
        return null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

I am getting this error: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.Timer.schedule(java.util.TimerTask, long)' on a null object reference 我收到此错误: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.Timer.schedule(java.util.TimerTask, long)' on a null object reference

If I remove the timer and import the stopMusic() method content into the ACTION_UP part - then it works fine, but the I don't have the 2 seconds requirement. 如果我删除计时器并将stopMusic()方法的内容导入ACTION_UP部分-则可以正常工作,但我没有2秒的要求。 It seems like I am unable to do a timer function within the ACTION_DOWN part. 看来我无法在ACTION_DOWN部分内执行计时器功能。 Is there a way for me to accomplish this differently? 我有办法以不同的方式来完成此工作吗? or is something in my code wrong? 还是我的代码有问题?

Thanks in advance. 提前致谢。

You have not initialized Your timer, first correct this error. 您尚未初始化计时器,请先更正此错误。 If then nothing works, come back: 如果没有任何效果,请返回:

Timer timer;

You have to initialize it: 您必须初始化它:

timer = new Timer();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android工具栏按钮错误-ViewPostImeInputStage ACTION_DOWN - Android Toolbar Button Error - ViewPostImeInputStage ACTION_DOWN 如何在Android onMediaButtonEvent中收听“ ACTION_DOWN”(按键)事件,以测量时间? - How to listen to “ACTION_DOWN” (key pressed) event in Android onMediaButtonEvent, in order to measure the time? 按钮OnClickListener给出ViewPostImeInputStage ACTION_DOWN错误 - Button OnClickListener giving ViewPostImeInputStage ACTION_DOWN error 如何在OnTouchListener中获取初始X,Y值(第一次触摸ACTION_DOWN) - How to get initial X, Y values (of first touch ACTION_DOWN) in OnTouchListener Android:如何关联来自同一拖动动作的ACTION_DOWN和ACTION_MOVE事件 - Android: How to correlate ACTION_DOWN and ACTION_MOVE events that come from the same drag motion 使RadioGroup选中ACTION_DOWN上的按钮 - make RadioGroup to check button on ACTION_DOWN 当我调用事件时,onTouchEvent的ACTION_DOWN MotionEvent中的代码引发异常 - code within onTouchEvent's ACTION_DOWN MotionEvent throws exception when i call the event onInterceptTouchEvent 只获得 ACTION_DOWN - onInterceptTouchEvent only gets ACTION_DOWN getAction()只给出ACTION_DOWN - getAction() gives only ACTION_DOWN onClick不执行任何操作……“ ViewPostImeInputStage ACTION_DOWN” - onClick not doing anything… “ViewPostImeInputStage ACTION_DOWN”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM