[問題] 使用範例程式碼編譯成功但不能執行
使用ADT內建的範例程式來編譯成功
不過使用模擬器或實際測試2.1的手機卻都不能執行
請問一下會是什麼問題呢?
錯誤訊息顯示:
Sorry! The application has stopped unexpectedly. Please try again.
package com.example.mp3test;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private static final String TAG = "MediaPlayerDemo";
private MediaPlayer mMediaPlayer;
private static final String MEDIA = "media";
private static final int LOCAL_AUDIO = 1;
private static final int STREAM_AUDIO = 2;
private static final int RESOURCES_AUDIO = 3;
private static final int LOCAL_VIDEO = 4;
private static final int STREAM_VIDEO = 5;
private String path;
private TextView tx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tx = new TextView(this);
setContentView(tx);
Bundle extras = getIntent().getExtras();
playAudio(extras.getInt(MEDIA));
}
private void playAudio(Integer media) {
try {
switch (media) {
case LOCAL_AUDIO:
/**
* TODO: Set the path variable to a local audio file path.
*/
path = "";
if (path == "") {
// Tell the user to provide an audio file URL.
Toast
.makeText(
MainActivity.this,
"Please edit MediaPlayer_Audio Activity, "
+ "and set the path variable to your audio file path."
+ " Your audio file must be stored on sdcard.",
Toast.LENGTH_LONG).show();
}
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
break;
case RESOURCES_AUDIO:
/**
* TODO: Upload a audio file to res/raw folder and provide
* its resid in MediaPlayer.create() method.
*/
mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);
mMediaPlayer.start();
}
tx.setText("Playing audio...");
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.42.216.70
※ 編輯: Dmitry 來自: 114.42.216.70 (04/06 14:43)
→
04/06 20:00, , 1F
04/06 20:00, 1F
→
04/07 12:27, , 2F
04/07 12:27, 2F
→
04/07 12:28, , 3F
04/07 12:28, 3F
→
04/13 00:37, , 4F
04/13 00:37, 4F