在 Android 4.3 中启动服务时出错

Posted

技术标签:

【中文标题】在 Android 4.3 中启动服务时出错【英文标题】:Error while starting a service in Android 4.3 【发布时间】:2013-12-17 04:43:49 【问题描述】:

我有一个服务类,我在其中定义了我的所有音乐播放/暂停功能。同一个类实现了 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;

【问题讨论】:

您使用的是光标还是任何列表?粘贴您的代码。 只有在 4.3 中才会崩溃有什么特别的原因吗? java.lang.ArrayIndexOutOfBoundsException: length=12; index=-1 这个错误说明你的错误.. 检查特定行并检查索引.. 我不知道与版本相关的问题.. :( 是哪一行?我找不到它,我已经在 try catch 中编写了所有代码,但它仍然崩溃...... 您在哪个操作上收到此错误? 【参考方案1】:

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

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

【讨论】:

我有源代码,我正在看那里,我认为它的 songIndex .. 谢谢会接受,如果它工作,songIndex 是 -1... 所以我可以从这里调试。再次感谢

以上是关于在 Android 4.3 中启动服务时出错的主要内容,如果未能解决你的问题,请参考以下文章

启动 Android 活动时出错

错误类型 3 - 启动活动时出错

安装应用程序后在 android studio 中启动活动时出错

启动新活动 Android 时出错

Android Studio 出错:无法在模拟器中启动 AVD

Visual Studio:Xamarin - 启动 Android 模拟器时出错