Question

I have a service class where I have defined my all music play/pause function. Same class implements the SensorEventListener, I am getting this error when I test it on Android4.3

Full Logcat :

        12-16 23:18:03.194: E/AndroidRuntime(966): java.lang.RuntimeException:
        Unable to   start service com.example.proximitybasedmediaplayer.PlayerService@41cf8d68 
        with Intent { cmp=com.example.proximitybasedmediaplayer/.PlayerService (has extras) }:
        java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
        12-17 00:33:40.754: E/AndroidRuntime(1341): FATAL EXCEPTION: main
        12-17 00:33:40.754: E/AndroidRuntime(1341): java.lang.RuntimeException: Unable to start service com.example.proximitybasedmediaplayer.PlayerService@41cf8d48 with Intent { cmp=com.example.proximitybasedmediaplayer/.PlayerService (has extras) }: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2721)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at android.app.ActivityThread.access$1900(ActivityThread.java:141)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at android.os.Handler.dispatchMessage(Handler.java:99)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at android.os.Looper.loop(Looper.java:137)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at android.app.ActivityThread.main(ActivityThread.java:5103)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at java.lang.reflect.Method.invokeNative(Native Method)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at java.lang.reflect.Method.invoke(Method.java:525)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at dalvik.system.NativeStart.main(Native Method)
        12-17 00:33:40.754: E/AndroidRuntime(1341): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at java.util.ArrayList.get(ArrayList.java:310)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at com.example.proximitybasedmediaplayer.PlayerService.initNotification(PlayerService.java:585)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at com.example.proximitybasedmediaplayer.PlayerService.onStartCommand(PlayerService.java:177)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2704)
        12-17 00:33:40.754: E/AndroidRuntime(1341):     ... 10 more

It work on 4.2 or lower version.

What logic make it work in 4.2 and not in 4.3? Is there any change in passing extras with an intent?

Moreover after crashing I see that the listener part written in service getting called. What does it mean, Service that throws error gets started? I am not able to understand the flow how things are getting called. I have written all my code under try catch, but it still getting crashed.(Only in 4.3 version)

Sorry for not providing any code as it's very big file.

Any help is appreciated.

Code :

    public void playSong(int songIndex) {
    // Play song
    try {

        initUI();

        mp.reset();
        mp.setDataSource(songsListingSD.get(songIndex).get("songPath"));
        mp.prepare();
        mp.start();
        // Displaying Song title
        String songTitle = songsListingSD.get(songIndex).get("songTitle");
        if(songTitle != null)
        {
        songTitleLabel.get().setText(songTitle);
        }
        // Changing Button Image to pause image
        if(btnPlay!=null)
        {   
        btnPlay.get().setImageResource(R.drawable.ic_media_pause);
        }
        // set Progress bar values
        songProgressBar.get().setProgress(0);
        songProgressBar.get().setMax(100);
        // Updating progress bar
        updateProgressBar();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    catch(Exception e)
    {e.printStackTrace();}
}

This method is getting called inside :

SensorEventListener proximityListener = new SensorEventListener(){

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onSensorChanged(SensorEvent event) {
                // TODO Auto-generated method stub
                Log.d("Debug", "Proximity Sensor");

                Random rand = new Random();
                try{
                currentSongIndex = rand.nextInt((songsListingSD.size() - 1) - 0 + 1) + 0;

                }
                catch(Exception e)
                {
                    e.printStackTrace();

                }
                try{
                playSong(currentSongIndex);
                    Log.d("Test","Test");
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

            }};

initUI part is :

private void initUI() {
    try{
    songTitleLabel = new WeakReference<TextView>(MainActivity.songTitle);
    songCurrentDurationLabel = new WeakReference<TextView>(
            MainActivity.songCurrentDurationLabel);
    songTotalDurationLabel = new WeakReference<TextView>(
            MainActivity.songTotalDurationLabel);

    btnPlay = new WeakReference<ImageView>(MainActivity.btnPlay);
    if(btnPlay!= null)
    {
        Log.d("Debug", "btnPlay not null11");
    }
    btnForward = new WeakReference<ImageView>(MainActivity.btnForward);
    btnBackward = new WeakReference<ImageView>(MainActivity.btnBackward);
    btnNext = new WeakReference<ImageView>(MainActivity.btnNext);
    btnPrevious = new WeakReference<ImageView>(MainActivity.btnPrevious);
    btnRepeat = new WeakReference<ImageButton>(MainActivity.btnRepeat);
    btnShuffle = new WeakReference<ImageButton>(MainActivity.btnShuffle);

    btnPlay.get().setOnClickListener(this);
    btnForward.get().setOnClickListener(this);
    btnBackward.get().setOnClickListener(this);
    btnNext.get().setOnClickListener(this);
    btnPrevious.get().setOnClickListener(this);
    btnRepeat.get().setOnClickListener(this);
    btnShuffle.get().setOnClickListener(this);
    // TODO Auto-generated method stub

    songProgressBar = new WeakReference<SeekBar>(
            MainActivity.songProgressBar);
    songProgressBar.get().setOnSeekBarChangeListener(this);
    }
    catch(Exception e){
        Log.d("ERROR","Inside initComp Service Class");
        e.printStackTrace();}
}

Variable are :

 private WeakReference<ImageButton> btnRepeat, btnShuffle;
private WeakReference<ImageView> btnPlay, btnForward, btnBackward, btnNext,
        btnPrevious;
private WeakReference<SeekBar> songProgressBar;
private WeakReference<TextView> songTitleLabel;
private WeakReference<TextView> songCurrentDurationLabel;
private WeakReference<TextView> songTotalDurationLabel;
public static MediaPlayer mp;
private Handler progressBarHandler = new Handler();;
private Utilities utils;
private int seekForwardTime = 5000; // 5000 milliseconds
private int seekBackwardTime = 5000; // 5000 milliseconds
private boolean isShuffle = false;
private boolean isRepeat = false;
private ArrayList<HashMap<String, String>> songsListingSD = new ArrayList<HashMap<String, String>>();

private ArrayList<MusicMetadata> songListingRecent = new ArrayList<MusicMetadata>();

public static int currentSongIndex = -1;
Was it helpful?

Solution

Do you have the source of com.example.proximitybasedmediaplayer.PlayerService.initNotification (PlayerService.java:585)? It tries to fetch from an ArrayList with index -1. -1 is the value returned by search-something methods when that something is not found. If you do have the source, find out what is searched for, most likely an argument is missing. If you don't have the source, you can fetch the apk file from the device with adb and decode the source with jad. The decoded source will not compile, but will be somehow readable.

An exception in the log does not mean the service cannot start. Probably the programmer preferred to catch an exception instead of checking for index==-1. And e.printStackTrace() is what most IDEs automatically insert into a catch block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top