所以我有这个代码:
public class StreamingMp3Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener, OnItemSelectedListener{
TextView textMsg, textPrompt;
ImageView imageView;
final String textSource = "http://www.sdads.ro/sdsad.php";
private ImageButton buttonPlayPause;
protected EditText editTextSongURL;
private MediaPlayer mediaPlayer;
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
imageView = (ImageView) findViewById(R.id.imagine);
textPrompt = (TextView)findViewById(R.id.textprompt);
textMsg = (TextView)findViewById(R.id.textmsg);
textMsg.post( new Runnable(){
public void run(){
URL textUrl;
try {
textUrl = new URL(textSource);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));
String StringBuffer;
String stringText = "";
while ((StringBuffer = bufferReader.readLine()) != null) {
stringText += StringBuffer;
}
bufferReader.close();
textMsg.setText(stringText);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
textMsg.setText(e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
textMsg.setText(e.toString());
}
textMsg.postDelayed(this,(1000*10));
}
});
initView();
spinner.setOnItemSelectedListener(new StreamingMp3Player());
}
private void initView() {
buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
buttonPlayPause.setOnClickListener(this);
editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
editTextSongURL.setText("Romantic FM");
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
}
public void onClick(View v) {
if(v.getId() == R.id.ButtonTestPlayPause){
try {
mediaPlayer.setDataSource("http://dfgdgfdgf:8080");
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
//mediaFileLengthInMilliseconds = mediaPlayer.getDuration();
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
//buttonPlayPause.setBackgroundColor(Color.RED);
buttonPlayPause.setImageResource(R.drawable.button_pause);
}else {
mediaPlayer.pause();
buttonPlayPause.setImageResource(R.drawable.button_play);
//System.out.println("Pauza");
}
}
}
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if(pos == 1) {
mediaPlayer.pause();
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
public void onCompletion(MediaPlayer mp) {
buttonPlayPause.setImageResource(R.drawable.button_play);
}
public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
// TODO Auto-generated method stub
}
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}
这段代码是我从微调器获取值的地方:
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if(pos == 1) {
mediaPlayer.pause();
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
它应该暂停媒体播放器,而不是我得到错误。这是来自 logcat:
05-27 12:39:05.554: E/AndroidRuntime(1432): FATAL EXCEPTION: main
05-27 12:39:05.554: E/AndroidRuntime(1432): java.lang.NullPointerException
05-27 12:39:05.554: E/AndroidRuntime(1432): at com.hrupin.streamingmedia.StreamingMp3Player.onItemSelected(StreamingMp3Player.java:161)
05-27 12:39:05.554: E/AndroidRuntime(1432): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
05-27 12:39:05.554: E/AndroidRuntime(1432): at android.widget.AdapterView.access$200(AdapterView.java:42)
05-27 12:39:05.554: E/AndroidRuntime(1432): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
05-27 12:39:05.554: E/AndroidRuntime(1432): at android.os.Handler.handleCallback(Handler.java:587)
05-27 12:39:05.554: E/AndroidRuntime(1432): at android.os.Handler.dispatchMessage(Handler.java:92)
05-27 12:39:05.554: E/AndroidRuntime(1432): at android.os.Looper.loop(Looper.java:123)
05-27 12:39:05.554: E/AndroidRuntime(1432): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-27 12:39:05.554: E/AndroidRuntime(1432): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 12:39:05.554: E/AndroidRuntime(1432): at java.lang.reflect.Method.invoke(Method.java:507)
05-27 12:39:05.554: E/AndroidRuntime(1432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-27 12:39:05.554: E/AndroidRuntime(1432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-27 12:39:05.554: E/AndroidRuntime(1432): at dalvik.system.NativeStart.main(Native Method)
那我做错了什么?当我选择应该暂停媒体播放器的微调器值时,是什么导致应用程序崩溃?