我正在尝试在录制音频时运行搜索栏。
这是我的代码。
public class ServerAudioSendActivity extends Activity {
private Button buttonPlayStop;
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private final Handler handler = new Handler();
// Here i override onCreate method.
//
// setContentView() method set the layout that you will see then
// the application will starts
//
// initViews() method i create to init views components.
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
initViews();
}
// This method set the setOnClickListener and method for it (buttonClick())
private void initViews() {
buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
buttonPlayStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttonClick();
}
});
// mediaPlayer = MediaPlayer.create(this, R.raw.sound41772);
seekBar = (SeekBar) findViewById(R.id.SeekBar01);
seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
seekChange(v);
return false;
}
});
}
public void startPlayProgressUpdater() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
} else {
mediaPlayer.pause();
buttonPlayStop.setText(getString(R.string.play_str));
seekBar.setProgress(0);
}
}
// This is event handler thumb moving event
private void seekChange(View v) {
if (mediaPlayer.isPlaying()) {
SeekBar sb = (SeekBar) v;
mediaPlayer.seekTo(sb.getProgress());
}
}
// This is event handler for buttonClick event
private void buttonClick() {
if (buttonPlayStop.getText() == getString(R.string.play_str)) {
buttonPlayStop.setText(getString(R.string.pause_str));
try {
mediaPlayer.start();
startPlayProgressUpdater();
} catch (IllegalStateException e) {
mediaPlayer.pause();
}
} else {
buttonPlayStop.setText(getString(R.string.play_str));
mediaPlayer.pause();
}
}
}
在 logcat 中给出这个错误
06-06 17:57:33.522: W/dalvikvm(1068): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-06 17:57:33.542: E/AndroidRuntime(1068): FATAL EXCEPTION: main
06-06 17:57:33.542: E/AndroidRuntime(1068): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sendaudiotoserver/com.sendaudiotoserver.ServerAudioSendActivity}: java.lang.NullPointerException
06-06 17:57:33.542: E/AndroidRuntime(1068): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-06 17:57:33.542: E/AndroidRuntime(1068): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-06 17:57:33.542: E/AndroidRuntime(1068): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-06 17:57:33.542: E/AndroidRuntime(1068): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-06 17:57:33.542: E/AndroidRuntime(1068): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 17:57:33.542: E/AndroidRuntime(1068): at android.os.Looper.loop(Looper.java:137)
06-06 17:57:33.542: E/AndroidRuntime(1068): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 17:57:33.542: E/AndroidRuntime(1068): at java.lang.reflect.Method.invokeNative(Native Method)
0 6-06 17:57:33.542: E/AndroidRuntime(1068): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 17:57:33.542: E/AndroidRuntime(1068): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 17:57:33.542: E/AndroidRuntime(1068): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
0 6-06 17:57:33.542: E/AndroidRuntime(1068): at dalvik.system.NativeStart.main(Native Method)
06-06 17:57:33.542: E/AndroidRuntime(1068): Caused by: java.lang.NullPointerException
06-06 17:57:33.542: E/AndroidRuntime(1068): at com.sendaudiotoserver.ServerAudioSendActivity.startPlayProgressUpdater(ServerAudioSendActivity.java:60)
06-06 17:57:33.542: E/AndroidRuntime(1068): at com.sendaudiotoserver.ServerAudioSendActivity.onCreate(ServerAudioSendActivity.java:32)
06-06 17:57:33.542: E/AndroidRuntime(1068): at android.app.Activity.performCreate(Activity.java:4465)
06-06 17:57:33.542: E/AndroidRuntime(1068): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-06 17:57:33.542: E/AndroidRuntime(1068): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-06 17:57:33.542: E/AndroidRuntime(1068): ... 11 more
请让我知道我要去哪里错了..
非常感谢拉胡尔