我在 S4 中研究接近传感器。更新到 4.3 后,我的应用程序开始崩溃。
这是代码示例:
这个方法写在一个服务类中
public void playSong(int songIndex) {
// Play song
try {
Log.d("Debug","playSong 1");
mp.reset();
Log.d("Debug","playSong 2");
mp.setDataSource(songsListingSD.get(songIndex).get("songPath"));
Log.d("Debug","playSong 3");
mp.prepare();
Log.d("Debug","playSong 4");
mp.start();
// Displaying Song title
Log.d("Debug","playSong 5");
String songTitle = songsListingSD.get(songIndex).get("songTitle");
Log.d("Debug","playSong songTitle = " + songTitle);
Log.d("Debug","playSong 6");
songTitleLabel.get().setText(songTitle);
Log.d("Debug","playSong 7");
// Changing Button Image to pause image
btnPlay.get().setImageResource(R.drawable.ic_media_pause); // :351
Log.d("Debug","playSong 8");
// 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();
}
}
我已经用 SensorEventListener 接口实现了服务类并实现了抽象方法。
这是代码:
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
Log.d("Player Service", "service called for sensor event, Next called1");
try{
Log.d("Debug"," check Here");
Random rand = new Random();
Log.d("Debug","sensor method 1");
currentSongIndex = rand
. nextInt((songsListingSD.size() - 1) - 0 + 1) + 0;
Log.d("Debug","sensor method 2");
Log.d("Debug"," Cur Index = " + currentSongIndex);
playSong(currentSongIndex);
Log.d("Debug","sensor method 3");
}
catch(Exception e){e.printStackTrace();}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
这是堆栈跟踪:
10-29 01:09:29.546: D/Player Service(19444): service called for sensor event, Next called1
10-29 01:09:29.546: D/Debug(19444): check Here
10-29 01:09:29.546: D/Debug(19444): sensor method 1
10-29 01:09:29.546: D/Debug(19444): sensor method 2
10-29 01:09:29.546: D/Debug(19444): Cur Index = 2
10-29 01:09:29.546: D/Debug(19444): playSong 1
10-29 01:09:29.546: D/Debug(19444): playSong 2
10-29 01:09:29.576: D/Debug(19444): playSong 3
10-29 01:09:29.585: D/Debug(19444): playSong 4
10-29 01:09:29.596: D/Debug(19444): playSong 5
10-29 01:09:29.596: D/Debug(19444): playSong songTitle = [SongsPK.info] Aashiqui 2 - 01 - Tum Hi Ho
10-29 01:09:29.596: D/Debug(19444): playSong 6
10-29 01:09:29.596: W/System.err(19444): java.lang.NullPointerException
10-29 01:09:29.596: W/System.err(19444): at com.example.audionplayerinservice.PlayerService.playSong(PlayerService.java:351)
10-29 01:09:29.606: W/System.err(19444): at com.example.audionplayerinservice.PlayerService.onSensorChanged(PlayerService.java:589)
10-29 01:09:29.606: W/System.err(19444): at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:371)
10-29 01:09:29.606: W/System.err(19444): at android.os.MessageQueue.nativePollOnce(Native Method)
10-29 01:09:29.606: W/System.err(19444): at android.os.MessageQueue.next(MessageQueue.java:132)
10-29 01:09:29.606: W/System.err(19444): at android.os.Looper.loop(Looper.java:124)
10-29 01:09:29.606: W/System.err(19444): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-29 01:09:29.606: W/System.err(19444): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 01:09:29.606: W/System.err(19444): at java.lang.reflect.Method.invoke(Method.java:525)
10-29 01:09:29.606: W/System.err(19444): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-29 01:09:29.606: W/System.err(19444): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-29 01:09:29.616: W/System.err(19444): at dalvik.system.NativeStart.main(Native Method)
注册接近侦听器的代码:
private WeakReference<ImageView> btnPlay
private WeakReference<TextView> songTitleLabel;
private SensorManager mSensorManager;
private Sensor mSensor;
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
mSensorManager.registerListener(this, mSensor,SensorManager.SENSOR_DELAY_NORMAL );
我正在 emulator 4.3 (Android) 中对此进行测试。如果我评论传感器部分,那么该应用程序正在运行。请帮助我进行调试,因为我找不到任何错误。提前致谢