我一直在研究一个非常简单的应用程序。只有一个音乐文件,一个播放按钮,没什么。我试图制作它以便在点击按钮时音乐开始播放然后我想让用户按住停止按钮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);
}
}
我收到此错误:java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.Timer.schedule(java.util.TimerTask, long)' on a null object reference
如果我删除计时器并将stopMusic()
方法内容导入ACTION_UP
部分 - 那么它运行正常,但我没有2秒的要求。好像我无法在ACTION_DOWN
部分内执行计时器功能。有没有办法让我以不同的方式完成这项工作?或者我的代码中有什么错误?
提前致谢。
答案 0 :(得分:1)
您尚未初始化计时器,请先更正此错误。如果没有任何作用,请回来:
Timer timer;
你必须初始化它:
timer = new Timer();