在Android 4.3中使用接近传感器

时间:2013-10-29 05:34:44

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

我正在研究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 );

我正在模拟器4.3(Android)中测试它。如果我评论传感器部件,那么应用程序正在运行。 请帮我调试,因为我找不到任何错误。 在此先感谢

1 个答案:

答案 0 :(得分:0)

请参阅WeakReference的文档。
无法保证WeakReference get()方法始终返回ImageView。当System调用垃圾收集器时,WeakReference中的ImageView可以被清除,因为ImageView具有WeakReference。当ImageView收集了垃圾时,get()方法只返回null 所以像null

那样做空检查
     ImageView imageView = btnPlay.get();
     if(imageView != bull) {
        imageView.setImageResource(R.drawable.ic_media_pause);
     }