我有一个服务类,在那里我定义了我的所有音乐播放/暂停功能。同一个类实现了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;
您有com.example.proximitybasedmediaplayer.PlayerService.initNotification (PlayerService.java:585)
的来源吗?它尝试从索引为-1的ArrayList中提取-1是在找不到某个东西时,search something方法返回的值。如果你有来源,找出要搜索的内容,很可能是缺少了一个论点。如果没有源代码,可以使用adb
从设备中获取apk文件,并使用jad对源代码进行解码。解码后的源代码不会编译,但会以某种方式可读。
日志中出现异常并不意味着服务无法启动。程序员可能更喜欢捕捉异常,而不是检查索引==-1。e.printStackTrace()是大多数IDE自动插入到catch块中的内容。