尝试调用虚方法' android.view.Window $ Callback null对象引用错误

时间:2016-08-23 01:20:21

标签: android

我有上述错误:

FATAL EXCEPTION: main
                                                                  Process: PID: 21721
                                                                  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{in.ddas.pretense/in.ddas.pretense.SideActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

这是我崩溃的活动:

public class SideActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView side_output;
    private SensorManager mySensorManager ;
    private Sensor LightSensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_side);

        //Introduce the buttons click!
    side_output = (TextView ) findViewById(R.id.txt_side_data);
    mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    LightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

        Button btn_start_p_poll = (Button) findViewById(R.id.btn_pos_poll_side);
        btn_start_p_poll.setOnClickListener(this);
        Button btn_start_n_poll = (Button) findViewById(R.id.btn_neg_poll_side);
        btn_start_n_poll.setOnClickListener(this);
        Button btn_start_l_poll = (Button) findViewById(R.id.btn_live_poll_side);
        btn_start_l_poll.setOnClickListener(this);
        Button stop_poll = (Button) findViewById(R.id.btn_stop_poll_side);
        stop_poll.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_pos_poll_side: {

                //Start reading LIGHT SENSOR Data, Handle Null Light Sensor Data
                if (LightSensor != null) {
                    mySensorManager.registerListener(
                            LightSensorListener,
                            LightSensor,
                            SensorManager.SENSOR_DELAY_NORMAL);

                } else {
                    side_output.setText("No Sensor Found!");
                }
                break;
            }

            case R.id.btn_neg_poll_side: {
                // do something for button 2 click
                break;
            }

            case R.id.btn_stop_poll_side: {
                // remove sensor listener
                mySensorManager.unregisterListener(LightSensorListener,
                        LightSensor);

                break;
            }

            case R.id.btn_live_poll_side: {
                // do something for button 2 click
                break;
            }
        }
    }

    //Process Android Sensor Event
    private final SensorEventListener LightSensorListener
            = new SensorEventListener() {

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

        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
                //textLIGHT_reading.setText("LIGHT: " + event.values[0]);
                side_output.append("Current Reading: " + String.valueOf(event.values[0]));
            }
        }

    };
}

我错过了什么? 感谢。

0 个答案:

没有答案