gpt4 book ai didi

java - 应用程序在按钮触摸时崩溃 - onTouch 方法错误?

转载 作者:行者123 更新时间:2023-12-01 14:53:05 25 4
gpt4 key购买 nike

当我单击“startButton”时,我的应用程序不断崩溃。我猜问题出在 onTouch 方法中的某个地方,但我已经尝试解决这个问题几个小时了;禁用 startButton.setOnTouchListener 内的不同内容不会产生任何影响。你能发现什么问题吗?

MediaPlayer mp;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
final Button startButton = (Button) this.findViewById(R.id.button1);
final TextView timeLeft = (TextView) this.findViewById(R.id.timeLeft);
MediaPlayer.create(getBaseContext(), R.raw.songthingy);


startButton.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {

mp.start();
timeLeft.setText("Status: Initiated");
startButton.setText("Stop");

startButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
mp.release();

}

});

new CountDownTimer(30000, 1000) {

public void onTick(long millisUntilFinished) {
timeLeft.setText("Status: Starting in... "
+ millisUntilFinished / 1000);
}

public void onFinish() {
timeLeft.setText("Status: Finished");
}
}.start();

mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
;
return true;
}
});

}

最佳答案

不存在有问题的异常堆栈,但我看到的一个问题是:

MediaPlayer mp;

指向null并且您正在调用

mp.start();

OnTouchListener中,这会导致NullPointerException

我认为你需要做的是:

 final TextView timeLeft = (TextView) this.findViewById(R.id.timeLeft);
mp= MediaPlayer.create(getBaseContext(), R.raw.songthingy);

关于java - 应用程序在按钮触摸时崩溃 - onTouch 方法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14636195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com