[英]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.