Исключение Null Pointer, подготовка аудио-проигрывателя

Я готовлю небольшой аудиоплеер и покажу список аудиозаписей с устройства, но всякий раз, когда я нажимаю какую-либо песню для воспроизведения, каждый раз получая ошибку.

Полный Logcat:

06-10 15:52:28.440: W/dalvikvm(3732): threadid=1: thread exiting with uncaught exception (group=0x41e0d2a0)
06-10 15:52:28.450: E/AndroidRuntime(3732): FATAL EXCEPTION: main
06-10 15:52:28.450: E/AndroidRuntime(3732): java.lang.IllegalStateException: Could not execute method of the activity
06-10 15:52:28.450: E/AndroidRuntime(3732): at android.view.View$1.onClick(View.java:3699)
06-10 15:52:28.450: E/AndroidRuntime(3732): at android.view.View.performClick(View.java:4223)
06-10 15:52:28.450: E/AndroidRuntime(3732): at android.view.View$PerformClick.run(View.java:17275)
06-10 15:52:28.450: E/AndroidRuntime(3732): at android.os.Handler.handleCallback(Handler.java:615)
06-10 15:52:28.450: E/AndroidRuntime(3732): at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 15:52:28.450: E/AndroidRuntime(3732): at android.os.Looper.loop(Looper.java:137)
06-10 15:52:28.450: E/AndroidRuntime(3732): at android.app.ActivityThread.main(ActivityThread.java:4921)
06-10 15:52:28.450: E/AndroidRuntime(3732): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 15:52:28.450: E/AndroidRuntime(3732): at java.lang.reflect.Method.invoke(Method.java:511)
06-10 15:52:28.450: E/AndroidRuntime(3732): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
06-10 15:52:28.450: E/AndroidRuntime(3732): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
06-10 15:52:28.450: E/AndroidRuntime(3732): at dalvik.system.NativeStart.main(Native Method)
06-10 15:52:28.450: E/AndroidRuntime(3732): Caused by: java.lang.reflect.InvocationTargetException
06-10 15:52:28.450: E/AndroidRuntime(3732): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 15:52:28.450: E/AndroidRuntime(3732): at java.lang.reflect.Method.invoke(Method.java:511)
06-10 15:52:28.450: E/AndroidRuntime(3732): at android.view.View$1.onClick(View.java:3694)
06-10 15:52:28.450: E/AndroidRuntime(3732): ... 11 more
06-10 15:52:28.450: E/AndroidRuntime(3732): Caused by: java.lang.NullPointerException
06-10 15:52:28.450: E/AndroidRuntime(3732): at com.example.syncing.MainActivity.songPicked(MainActivity.java:98)
06-10 15:52:28.450: E/AndroidRuntime(3732): ... 14 more
06-10 15:52:38.065: I/Process(3732): Sending signal. PID: 3732 SIG: 9

проверьте мой полный исходный код MainActivity.java:-

public class MainActivity extends Activity implements MediaPlayerControl {

 //song list variables
 private ArrayList<song> songList;
 private ListView songView;

 //service
 private MusicService musicSrv;
 private Intent **********;
 //binding
 private boolean musicBound=false;

 //controller
 private MusicController controller;

 //activity and playback pause flags
 private boolean paused=false, playbackPaused=false;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 //retrieve list view
 songView = (ListView)findViewById(R.id.song_list);
 //instantiate list
 songList = new ArrayList<song>();
 //get songs from device
 getSongList();
 //sort alphabetically by title
 Collections.sort(songList, new Comparator<song>(){
 public int compare(Song a, Song b){
 return a.getTitle().compareTo(b.getTitle());
 }
 });
 //create and set adapter
 SongAdapter songAdt = new SongAdapter(this, songList);
 songView.setAdapter(songAdt);

 //setup controller
 setController();
 }

 //connect to the service
 private ServiceConnection musicConnection = new ServiceConnection(){

 @Override
 public void onServiceConnected(ComponentName name, IBinder service) {
 MusicBinder binder = (MusicBinder)service;
 //get service
 musicSrv = binder.getService();
 //pass list
 musicSrv.setList(songList);
 musicBound = true;
 }

 @Override
 public void onServiceDisconnected(ComponentName name) {
 musicBound = false;
 }
 };

 //start and bind the service when the activity starts
 @Override
 protected void onStart() {
 super.onStart();
 if(**********==null){
 ********** = new Intent(this, MusicService.class);
 bindService(**********, musicConnection, Context.BIND_AUTO_CREATE);
 startService(**********);
 }
 }

 //user song select
 public void songPicked(View view){
 musicSrv.setSong(Integer.parseInt(view.getTag().toString()));
 musicSrv.playSong();
 if(playbackPaused){
 setController();
 playbackPaused=false;
 }
 controller.show(0);
 }

 //method to retrieve song info from device
 public void getSongList(){
 //query external audio
 ContentResolver musicResolver = getContentResolver();
 Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
 Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
 //iterate over results if valid
 if(musicCursor!=null && musicCursor.moveToFirst()){
 //get columns
 int titleColumn = musicCursor.getColumnIndex
 (android.provider.MediaStore.Audio.Media.TITLE);
 int idColumn = musicCursor.getColumnIndex
 (android.provider.MediaStore.Audio.Media._ID);
 int artistColumn = musicCursor.getColumnIndex
 (android.provider.MediaStore.Audio.Media.ARTIST);
 //add songs to list
 do {
 long thisId = musicCursor.getLong(idColumn);
 String thisTitle = musicCursor.getString(titleColumn);
 String thisArtist = musicCursor.getString(artistColumn);
 songList.add(new Song(thisId, thisTitle, thisArtist));
 } 
 while (musicCursor.moveToNext());
 }
 }

 @Override
 public boolean canPause() {
 return true;
 }

 @Override
 public boolean canSeekBackward() {
 return true;
 }

 @Override
 public boolean canSeekForward() {
 return true;
 }

 @Override
 public int getAudioSessionId() {
 return 0;
 }

 @Override
 public int getBufferPercentage() {
 return 0;
 }

 @Override
 public int getCurrentPosition() {
 if(musicSrv!=null && musicBound && musicSrv.isPng())
 return musicSrv.getPosn();
 else return 0;
 }

 @Override
 public int getDuration() {
 if(musicSrv!=null && musicBound && musicSrv.isPng())
 return musicSrv.getDur();
 else return 0;
 }

 @Override
 public boolean isPlaying() {
 if(musicSrv!=null && musicBound)
 return musicSrv.isPng();
 return false;
 }

 @Override
 public void pause() {
 playbackPaused=true;
 musicSrv.pausePlayer();
 }

 @Override
 public void seekTo(int pos) {
 musicSrv.seek(pos);
 }

 @Override
 public void start() {
 musicSrv.go();
 }

 //set the controller up
 private void setController(){
 controller = new MusicController(this);
 //set previous and next button listeners
 controller.setPrevNextListeners(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 playNext();
 }
 }, new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 playPrev();
 }
 });
 //set and show
 controller.setMediaPlayer(this);
 controller.setAnchorView(findViewById(R.id.song_list));
 controller.setEnabled(true);
 }

 private void playNext(){
 musicSrv.playNext();
 if(playbackPaused){ 
 setController();
 playbackPaused=false;
 }
 controller.show(0);
 }

 private void playPrev(){
 musicSrv.playPrev();
 if(playbackPaused){
 setController();
 playbackPaused=false;
 }
 controller.show(0);
 }

 @Override
 protected void onPause(){
 super.onPause();
 paused=true;
 }

 @Override
 protected void onResume(){
 super.onResume();
 if(paused){
 setController();
 paused=false;
 }
 }

 @Override
 protected void onStop() {
 controller.hide();
 super.onStop();
 }

 @Override
 protected void onDestroy() {
 stopService(**********);
 musicSrv=null;
 super.onDestroy();
 }

}
</song></song></song>
1 ответ

Вы не установили тег, поэтому его вид не имеет. Вы получаете null на view.getTag(). ToString()

Или поместите где-нибудь setTag в это представление или проверьте перед вызовом, если оно равно null:

if(view.getTag() != null) {
 ...
}

licensed under cc by-sa 3.0 with attribution.