1

1 回のアクティビティで GPS データと加速度計データを読み取ることはできますか? データを読み込んでテキストビューに入れたい。加速度計のデータは既に読み取っていますが、このコードをアクティビティに追加するとアプリケーションがクラッシュします

public class MainActivity extends Activity implements SensorEventListener {
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    valuesInitialization();
    sensorsInitialization();

}

private void valuesInitialization()
{
    Config.mContext = this;
    mInitialized = false;
    mLastAccelerometer[0] = INITVALUE;
    mLastAccelerometer[1] = INITVALUE;
    mLastAccelerometer[2] = INITVALUE;
    mLastGyroscope[0] = INITVALUE;
    mLastGyroscope[1] = INITVALUE;
    mLastGyroscope[2] = INITVALUE;
    mLastOrientation[0] = INITVALUE;
    mLastOrientation[1] = INITVALUE;
    mLastOrientation[2] = INITVALUE;
}

private void sensorsInitialization() 
{
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
    mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);

    mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}

private void registListener() 
{
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_NORMAL);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
}

protected void onResume() 
{
    super.onResume();
    registListener();
}

protected void onPause() 
{
    super.onPause();
    mSensorManager.unregisterListener(this);
    }
}

問題を解決する方法はありますか?前もって感謝します

エラーログ:ここに画像の説明を入力 ここに画像の説明を入力

4

3 に答える 3

2

以下に示すように、クラスに Location Listener を実装する必要があります。

public class LocationListener implements android.location.LocationListener {
    final String LOG_LABEL = "Location Listener>>";

    @Override
    public void onLocationChanged(Location location) {
        Log.d(util.TAG,LOG_LABEL+ "Location Changed");
        if (location != null)
        {
            double longitude = location.getLongitude();
            Log.d(util.TAG,LOG_LABEL+ "Longitude:" + longitude);
            Toast.makeText(getApplicationContext(),"Long::"+longitude,Toast.LENGTH_SHORT).show();
            double latitude = location.getLatitude();
            Toast.makeText(getApplicationContext(),"Lat::"+latitude,Toast.LENGTH_SHORT).show();
            Log.d(util.TAG,LOG_LABEL+ "Latitude:" + latitude);

            cleanUp();

        }
    }

また、以下に示すようにロケーションリスナーを定義します

private android.location.LocationListener locListener;

次に、リスナーを開始するには、次のようにします。

this.locListener = new LocationListener();

そして、経度/緯度の値を取得したら、以下に示すようにクリーンアップすることを忘れないでください

cleanUp()
{
     // This needs to be done to stop getting the location data and save the
    // battery power.
    if (null != this.locListener && null != locationManager)
    {
        locationManager.removeUpdates(this.locListener);
        this.locListener = null;
    }
}
于 2013-09-24T12:28:21.980 に答える