onLongClickListener发布android
我试图通过以下方式指示用户何时释放长时间单击-- Java代码:onLongClickListener发布android,android,Android,我试图通过以下方式指示用户何时释放长时间单击-- Java代码: 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.*
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;
}
};
}
我试着做一个反应时间的小游戏,当你长按它,在随机数秒后,它会发出声音,当你听到声音时,当声音产生时,你必须松开按钮,它还创建了一个计时器,当你松开按钮时,它会告诉你听到声音和松开按钮需要多少时间
我的问题是,代码不工作,我在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)
错误在这一行“button1.setTextWait for sound and release”;但是当我试图删除这一行时,它给出了另一个错误
我怎样才能修好它
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);
}
您忽略了全局按钮1,而选择了本地按钮
将此用作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);
}
首先声明两个按钮对象,一个是全局对象,一个是本地对象,作为最终对象 所以请删除本地一个使用全局一个 复制粘贴我下面的代码它肯定会工作
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;
}
};
}
你知道什么是NullPointerException吗?随机删除行不是很有条理。理解您的问题。删除导致问题的行不是随机的。我最喜欢的方法是通过破坏更多的东西来调试。brother总是试图理解您的问题,更好的方法是使用调试器,如果您仍然无法找到解决方案,请继续stackoverflow@keyser事实上我不知道是什么:@r2DoesInc我只是删除了它,试图了解什么是我还不能理解的问题:因为我是新的说要等8分钟:P@Gadab确保您了解什么是错误的以及为什么这样做有效。