在Android 4.3中启动服务时出错

时间:2013-12-17 04:43:50

标签: android android-service android-4.3-jelly-bean

我有一个服务类,我已经定义了我的所有音乐播放/暂停功能。同一个类实现了SensorEventListener,我在Android4.3上测试时遇到了这个错误

完整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

适用于4.2或更低版本。

什么逻辑使它在4.2而不是4.3中工作?有意图传递额外内容有任何变化吗?

此外,在崩溃之后,我看到在服务中写入的监听器部分被调用。这是什么意思,抛出错误的服务开始了?我无法理解事情被调用的流程。我已经在try catch下编写了所有代码,但它仍然崩溃了。(仅在4.3版本中)

很抱歉没有提供任何代码,因为它是一个非常大的文件。

感谢任何帮助。

代码:

    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();}
}

此方法在内部调用:

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部分是:

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();}
}

变量是:

 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;

1 个答案:

答案 0 :(得分:0)

你有com.example.proximitybasedmediaplayer.PlayerService.initNotification (PlayerService.java:585)的来源吗?它尝试从索引为-1的ArrayList中获取。 -1是search-something方法返回的值,当找不到该内容时。如果你确实有源,找出搜索的内容,很可能缺少一个参数。如果您没有源,可以使用adb从设备获取apk文件,并使用jad解码源。解码后的源代码不会编译,但会以某种方式读取。

日志中的异常并不意味着服务无法启动。程序员可能更喜欢捕获异常而不是检查index == - 1。而e.printStackTrace()是大多数IDE自动插入到catch块中的。