私は最近、音楽アプリを構築しており、いくつかのソース コードを使用して、自分の仕様に合わせてカスタマイズしています。編集とデバッグを終了すると、起動時に一時停止します。
デバッグ:
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2306
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2356
ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 150
ActivityThread$H.handleMessage(Message) line: 1244
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 5195
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 795
ZygoteInit.main(String[]) line: 562
NativeStart.main(String[]) line: not available [native method]
Logcat ログ(ここに表示されないようです)
誰かが私のコードを見て、どこが間違っているのか教えてもらえますか?
これが私のプロジェクトクラスコードです:
package com.timmo.tampmusicplayer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class NowPlaying extends Activity implements OnCompletionListener, SeekBar.OnSeekBarChangeListener {
private ImageButton btnPlay;
private ImageButton btnNext;
private ImageButton btnPrevious;
private ImageButton btnPlaylist;
private ImageButton btnRepeat;
private ImageButton btnShuffle;
private SeekBar songProgressBar;
private TextView songTitleLabel;
private TextView songCurrentDurationLabel;
private TextView songTotalDurationLabel;
// Media Player
private MediaPlayer mp;
// Handler to update UI timer, progress bar etc,.
private Handler mHandler = new Handler();;
private SongsManager songManager;
private Utilities utils;
private int seekForwardTime = 5000; // 5000 milliseconds
private int seekBackwardTime = 5000; // 5000 milliseconds
private int currentSongIndex = 0;
private boolean isShuffle = false;
private boolean isRepeat = false;
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nowplaying);
// All player buttons
btnPlay = (ImageButton) findViewById(R.id.bplay);
btnNext = (ImageButton) findViewById(R.id.bnext);
btnPrevious = (ImageButton) findViewById(R.id.bprevious);
btnPlaylist = (ImageButton) findViewById(R.id.bLibrary);
btnRepeat = (ImageButton) findViewById(R.id.brepeat);
btnShuffle = (ImageButton) findViewById(R.id.bshuffle);
songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
songTitleLabel = (TextView) findViewById(R.id.songTitle);
songCurrentDurationLabel = (TextView) findViewById(R.id.tvtimecurrent);
songTotalDurationLabel = (TextView) findViewById(R.id.tvtimetotal);
// Mediaplayer
mp = new MediaPlayer();
songManager = new SongsManager();
utils = new Utilities();
// Listeners
songProgressBar.setOnSeekBarChangeListener(this); // Important
mp.setOnCompletionListener(this); // Important
// Getting all songs list
songsList = songManager.getPlayList();
// By default play first song
playSong(0);
/**
* Play button click event plays a song and changes button to pause
* image pauses a song and changes button to play image
* */
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// check for already playing
if (mp.isPlaying()) {
if (mp != null) {
mp.pause();
// Changing button image to play button
btnPlay.setImageResource(R.drawable.play);
}
} else {
// Resume song
if (mp != null) {
mp.start();
// Changing button image to pause button
btnPlay.setImageResource(R.drawable.pause);
}
}
}
});
/**
* Forward button click event Forwards song specified seconds
* */
btnNext.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// get current song position
int currentPosition = mp.getCurrentPosition();
// check if seekForward time is lesser than song duration
if (currentPosition + seekForwardTime <= mp.getDuration()) {
// forward song
mp.seekTo(currentPosition + seekForwardTime);
} else {
// forward to end position
mp.seekTo(mp.getDuration());
}
return true;
}
});
/**
* Backward button click event Backward song to specified seconds
* */
btnPrevious.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
// get current song position
int currentPosition = mp.getCurrentPosition();
// check if seekBackward time is greater than 0 sec
if (currentPosition - seekBackwardTime >= 0) {
// forward song
mp.seekTo(currentPosition - seekBackwardTime);
} else {
// backward to starting position
mp.seekTo(0);
}
return true;
}
});
/**
* Next button click event Plays next song by taking currentSongIndex +
* 1
* */
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// check if next song is there or not
if (currentSongIndex < (songsList.size() - 1)) {
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
} else {
// play first song
playSong(0);
currentSongIndex = 0;
}
}
});
/**
* Back button click event Plays previous song by currentSongIndex - 1
* */
btnPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (currentSongIndex > 0) {
playSong(currentSongIndex - 1);
currentSongIndex = currentSongIndex - 1;
} else {
// play last song
playSong(songsList.size() - 1);
currentSongIndex = songsList.size() - 1;
}
}
});
/**
* Button Click event for Repeat button Enables repeat flag to true
* */
btnRepeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (isRepeat) {
isRepeat = false;
Toast.makeText(getApplicationContext(), "Repeat is OFF", Toast.LENGTH_SHORT).show();
btnRepeat.setImageResource(R.drawable.norepeat);
} else {
// make repeat to true
isRepeat = true;
Toast.makeText(getApplicationContext(), "Repeat is ON", Toast.LENGTH_SHORT).show();
// make shuffle to false
isShuffle = false;
btnRepeat.setImageResource(R.drawable.repeat);
btnShuffle.setImageResource(R.drawable.noshuffle);
}
}
});
/**
* Button Click event for Shuffle button Enables shuffle flag to true
* */
btnShuffle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (isShuffle) {
isShuffle = false;
Toast.makeText(getApplicationContext(), "Shuffle is OFF", Toast.LENGTH_SHORT).show();
btnShuffle.setImageResource(R.drawable.noshuffle);
} else {
// make repeat to true
isShuffle = true;
Toast.makeText(getApplicationContext(), "Shuffle is ON", Toast.LENGTH_SHORT).show();
// make shuffle to false
isRepeat = false;
btnShuffle.setImageResource(R.drawable.noshuffle);
btnRepeat.setImageResource(R.drawable.norepeat);
}
}
});
/**
* Button Click event for Play list click event Launches list activity
* which displays list of songs
* */
btnPlaylist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), Library.class);
startActivityForResult(i, 100);
}
});
}
/**
* Receiving song index from playlist view and play the song
* */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
currentSongIndex = data.getExtras().getInt("songIndex");
// play selected song
playSong(currentSongIndex);
}
}
/**
* Function to play a song
*
* @param songIndex
* - index of song
* */
public void playSong(int songIndex) {
// Play song
try {
mp.reset();
mp.setDataSource(songsList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();
// Displaying Song title
String songTitle = songsList.get(songIndex).get("songTitle");
songTitleLabel.setText(songTitle);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Update timer on seekbar
* */
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
/**
* Background Runnable thread
* */
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
// Displaying Total Duration time
songTotalDurationLabel.setText("" + utils.milliSecondsToTimer(totalDuration));
// Displaying time completed playing
songCurrentDurationLabel.setText("" + utils.milliSecondsToTimer(currentDuration));
// Updating progress bar
int progress = (int) (utils.getProgressPercentage(currentDuration, totalDuration));
// Log.d("Progress", ""+progress);
songProgressBar.setProgress(progress);
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};
/**
*
* */
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
}
/**
* When user starts moving the progress handler
* */
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// remove message Handler from updating progress bar
mHandler.removeCallbacks(mUpdateTimeTask);
}
/**
* When user stops moving the progress hanlder
* */
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mHandler.removeCallbacks(mUpdateTimeTask);
int totalDuration = mp.getDuration();
int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);
// forward or backward to certain seconds
mp.seekTo(currentPosition);
// update timer progress again
updateProgressBar();
}
/**
* On Song Playing completed if repeat is ON play same song again if shuffle
* is ON play random song
* */
@Override
public void onCompletion(MediaPlayer arg0) {
// check for repeat is ON or OFF
if (isRepeat) {
// repeat is on play same song again
playSong(currentSongIndex);
} else if (isShuffle) {
// shuffle is on - play a random song
Random rand = new Random();
currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
playSong(currentSongIndex);
} else {
// no repeat or shuffle ON - play next song
if (currentSongIndex < (songsList.size() - 1)) {
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
} else {
// play first song
playSong(0);
currentSongIndex = 0;
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
}
Library.java
package com.timmo.tampmusicplayer;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class Library extends ListActivity {
// Songs list
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
SongsManager plm = new SongsManager();
// get all songs from sdcard
this.songsList = plm.getPlayList();
// looping through playlist
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
R.id.songTitle });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int songIndex = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),
NowPlaying.class);
// Sending songIndex to PlayerActivity
in.putExtra("songIndex", songIndex);
setResult(100, in);
// Closing PlayListView
finish();
}
});
}
}
理にかなっていることを確認する限り、何でも結構です
ログキャット
3-22 23:53:21.335: E/AndroidRuntime(25591): FATAL EXCEPTION: main
03-22 23:53:21.335: E/AndroidRuntime(25591): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.timmo.tampmusicplayer/com.timmo.tampmusicplayer.NowPlaying}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.ImageButton
03-22 23:53:21.335: E/AndroidRuntime(25591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
03-22 23:53:21.335: E/AndroidRuntime(25591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
03-22 23:53:21.335: E/AndroidRuntime(25591): at android.app.ActivityThread.access$600(ActivityThread.java:150)
03-22 23:53:21.335: E/AndroidRuntime(25591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
03-22 23:53:21.335: E/AndroidRuntime(25591): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 23:53:21.335: E/AndroidRuntime(25591): at android.os.Looper.loop(Looper.java:137)
03-22 23:53:21.335: E/AndroidRuntime(25591): at android.app.ActivityThread.main(ActivityThread.java:5195)
03-22 23:53:21.335: E/AndroidRuntime(25591): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 23:53:21.335: E/AndroidRuntime(25591): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 23:53:21.335: E/AndroidRuntime(25591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-22 23:53:21.335: E/AndroidRuntime(25591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-22 23:53:21.335: E/AndroidRuntime(25591): at dalvik.system.NativeStart.main(Native Method)
03-22 23:53:21.335: E/AndroidRuntime(25591): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.ImageButton
03-22 23:53:21.335: E/AndroidRuntime(25591): at com.timmo.tampmusicplayer.NowPlaying.onCreate(NowPlaying.java:54)
03-22 23:53:21.335: E/AndroidRuntime(25591): at android.app.Activity.performCreate(Activity.java:5104)
03-22 23:53:21.335: E/AndroidRuntime(25591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-22 23:53:21.335: E/AndroidRuntime(25591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
03-22 23:53:21.335: E/AndroidRuntime(25591): ... 11 more