SDカードのオーディオファイルを読み取り、スタートボタンを押すと再生するアプリを開発しています。奇妙なことに、アプリは私のDroid RAZRで正常に動作しますが、他のデバイスではクラッシュします。私は父のDroidRAZRで試してみましたが、クラッシュします。また、友人のSamsungGalaxyS2や古いDroidIncredibleでもクラッシュします。携帯電話のSDカードをIncredibleに挿入すると、一度は機能しましたが、お父さんに挿入しても同じ結果は得られませんでした。
これは、カーソルを持つメソッドのコードです。
private void init_phone_music_grid() {
System.gc();
String[] proj = { MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST }; // was originally set to MediaStore.Video.Media.ARTIST
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, MediaStore.Audio.Media.DURATION + ">= 65000", null, null );
count = musiccursor.getCount(); //causes force close, when plugged in sometimes; not an issue with StateCheck class
musiclist = (ListView) findViewById(R.id.lvTrackList);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
これは、OnCreateメソッドのコードです。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeVars();
init_phone_music_grid();
//countDown();
trackList.setCacheColorHint(Color.GRAY);
start.setEnabled(false);
pause.setEnabled(false);
reset.setEnabled(false);
trackList.setVisibility(View.INVISIBLE);
instructions.setVisibility(View.VISIBLE);
//*** USED FOR TESTING!!! ***//
Calendar date = Calendar.getInstance();
if(date.get(Calendar.DAY_OF_YEAR) >= 6) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setCancelable(false);
builder1.setMessage("This test version of Musercise has ended! Buy the real one if you want to use it!");
builder1.setPositiveButton("Close",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
finish();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
// END TESTING
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
chrono.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
chrono.start();
for(int i = 0; i <= trackList.getCount() - 1; i++) {
randomArray[i] = i;
}
shuffleArray(randomArray);
if(!isPaused) {
MyTimerTask myTask = new MyTimerTask();
timer = new Timer();
timer.schedule(myTask, (totalTimeLong)*60000);
}else if(resumeNeeded){
MyTimerTask myTask = new MyTimerTask();
timer = new Timer();
timer.schedule(myTask, timeRemainingAfterPause);
resumeNeeded = false;
pause.setEnabled(true);
}
if(!mMediaPlayer.isPlaying()) {
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(randomArray[0]);
String filename = musiccursor.getString(music_column_index);
try {
mMediaPlayer.setDataSource(filename);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mMediaPlayer.start();
}
if(mMediaPlayer.isPlaying()) {
includeWarmUp.setEnabled(false);
includeCoolDown.setEnabled(false);
test.setEnabled(false);
start.setEnabled(false);
trackList.setVisibility(View.VISIBLE);
instructions.setText("Track List:");
}
isPaused = false;
drawer.setBackgroundResource(R.drawable.run);
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isPaused = true;
resumeNeeded = true;
timeWhenStopped = chrono.getBase() - SystemClock.elapsedRealtime();
chrono.stop();
mMediaPlayer.pause();
long elapsedMillis = SystemClock.elapsedRealtime() - chrono.getBase();
timer.cancel();
timeRemainingAfterPause = totalTimeLong*60000 - elapsedMillis;
start.setEnabled(true);
pause.setEnabled(false);
drawer.setBackgroundResource(R.drawable.pause);
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
chrono.stop();
timeWhenStopped = 0;
chrono.setBase(SystemClock.elapsedRealtime());
mMediaPlayer.reset();
start.setTextColor(Color.BLACK);
pause.setTextColor(Color.BLACK);
includeWarmUp.setEnabled(true);
includeCoolDown.setEnabled(true);
test.setEnabled(true);
start.setEnabled(true);
pause.setEnabled(true);
trackList.setEnabled(true);
drawer.setBackgroundResource(R.drawable.ic_launcher);
}
});
test.setOnSeekBarChangeListener(this);
}
クラッシュしたときのLogcat:
01-03 19:41:01.678: E/AndroidRuntime(499): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-03 19:41:01.678: E/AndroidRuntime(499): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
01-03 19:41:01.678: E/AndroidRuntime(499): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
01-03 19:41:01.678: E/AndroidRuntime(499): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
01-03 19:41:01.678: E/AndroidRuntime(499): at android.database.CursorWrapper.getString(CursorWrapper.java:135)
01-03 19:41:01.678: E/AndroidRuntime(499): at com.flannigan.musercise.MuserciseActivity$3.onClick(MuserciseActivity.java:133)
01-03 19:41:01.678: E/AndroidRuntime(499): at android.view.View.performClick(View.java:2408)
01-03 19:41:01.678: E/AndroidRuntime(499): at android.view.View$PerformClick.run(View.java:8816)
01-03 19:41:01.678: E/AndroidRuntime(499): at android.os.Handler.handleCallback(Handler.java:587)
01-03 19:41:01.678: E/AndroidRuntime(499): at android.os.Handler.dispatchMessage(Handler.java:92)
01-03 19:41:01.678: E/AndroidRuntime(499): at android.os.Looper.loop(Looper.java:123)
01-03 19:41:01.678: E/AndroidRuntime(499): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-03 19:41:01.678: E/AndroidRuntime(499): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 19:41:01.678: E/AndroidRuntime(499): at java.lang.reflect.Method.invoke(Method.java:521)
01-03 19:41:01.678: E/AndroidRuntime(499): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-03 19:41:01.678: E/AndroidRuntime(499): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-03 19:41:01.678: E/AndroidRuntime(499): at dalvik.system.NativeStart.main(Native Method)