片段崩溃中的复选框

时间:2013-12-17 10:35:30

标签: android android-fragments android-sensors android-checkbox

我的片段中有一个复选框,我希望如果检查可以使用传感器打开蓝牙。这就是我正在做的事情:

片段的onCreate里面:

// Sensor manager 
        sensorManager = (SensorManager) getActivity().getSystemService(getActivity().SENSOR_SERVICE);
        lastUpdate = System.currentTimeMillis();

        // Radio buttons
        check = (CheckBox)v.findViewById(R.id.check1);


        check .setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                    if(check.isChecked()){ //do this}
                        onSensorChanged(null); // the error
                }
            }
         });

然后:

@Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onSensorChanged(SensorEvent event) {    
        // TODO Auto-generated method stub   
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                getBlueOn(event); 
        }

    }
    private void getBlueOn(SensorEvent event) {
        //myVib.vibrate(70);
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        float[] values = event.values;
        // Movement
        float x = values[0];
        float y = values[1];
        float z = values[2];

        float accelationSquareRoot = (x * x + y * y + z * z)
            / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
        long actualTime = System.currentTimeMillis();
        if (accelationSquareRoot >= 2) //
        {
          if (actualTime - lastUpdate < 200) {
            return;
          }
          lastUpdate = actualTime;
          mBluetoothAdapter.enable(); 
          Toast.makeText(getActivity(), "Blue on", Toast.LENGTH_SHORT)
          .show();
        }
      }

    @Override
    public void onResume() {
        super.onResume();
        // register this class as a listener for the orientation and
        // accelerometer sensors
        sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);
      }

当然在onSensorChanged(null);中发生了崩溃。我不知道在那里写什么来调用方法...复选框已经被声明为public CheckBox check;所以它不是问题..任何帮助? logcat的:

12-17 11:42:16.979: E/AndroidRuntime(22980): FATAL EXCEPTION: main
12-17 11:42:16.979: E/AndroidRuntime(22980): Process: com.dd.androreboot, PID: 22980
12-17 11:42:16.979: E/AndroidRuntime(22980): java.lang.NullPointerException
12-17 11:42:16.979: E/AndroidRuntime(22980):    at com.dd.androreboot.devicecontrolfragment.getBlueOn(devicecontrolfragment.java:373)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at com.dd.androreboot.devicecontrolfragment.access$1(devicecontrolfragment.java:370)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at com.dd.androreboot.devicecontrolfragment$8.onClick(devicecontrolfragment.java:340)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at android.view.View.performClick(View.java:4438)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at android.widget.CompoundButton.performClick(CompoundButton.java:100)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at android.view.View$PerformClick.run(View.java:18422)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at android.os.Handler.handleCallback(Handler.java:733)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at android.os.Looper.loop(Looper.java:136)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at android.app.ActivityThread.main(ActivityThread.java:5017)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at java.lang.reflect.Method.invokeNative(Native Method)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at java.lang.reflect.Method.invoke(Method.java:515)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-17 11:42:16.979: E/AndroidRuntime(22980):    at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案