错误:(22,30)错误:不兼容的类型:对象无法转换为传感器

时间:2017-10-03 12:59:02

标签: android

尝试显示设备中可用的传感器......这是我的代码,我很难知道该做什么......

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private SensorManager sMgr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView sensorsData = (TextView) findViewById(R.id.textView1);
        sMgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
        List list = sMgr.getSensorList(Sensor.TYPE_ALL);
        StringBuilder data = new StringBuilder();
        for (Sensor sensor : list) {
            data.append(sensor.getName() + "\n");
            data.append(sensor.getVendor() + "\n");
            data.append(sensor.getVersion() + "\n");
        }
        sensorsData.setText(data);
    }
}

,错误就在这一行

for (Sensor sensor : list) {

1 个答案:

答案 0 :(得分:2)

使用

<?php
$dateString = '2016-10-30 01:30:00'; // start time
$dateBefore = \DateTime::createFromFormat('Y-m-d H:i:s', $dateString, new DateTimeZone('Europe/Paris'));
var_dump($dateBefore);
$d = clone $dateBefore;
$d->setTimeZone(new \DateTimeZone('UTC'));
var_dump($d); // 23:30

echo PHP_EOL."------".PHP_EOL;

$dateBefore->add(new DateInterval('PT1H'))->format("Y-m-d H:i:s"); // add one hour without skipping
var_dump($dateBefore);
$d = clone $dateBefore;
$d->setTimeZone(new \DateTimeZone('UTC'));
var_dump($d); // 00:30

echo PHP_EOL."------".PHP_EOL;

$dateString = '2016-10-30 02:30:00';
$dateBefore = \DateTime::createFromFormat('Y-m-d H:i:s', $dateString, new DateTimeZone('Europe/Paris'));
var_dump($dateBefore);
$dateBefore->setTimeZone(new \DateTimeZone('UTC'));
var_dump($dateBefore); // 01:30, uses DST

而不是

List<Sensor> list = sMgr.getSensorList(Sensor.TYPE_ALL);