我正在进行一个在安卓平台上使用加速度计传感器获取数据的项目。但是,在最后三行代码中出现了错误代码。具体来说,findViewById不能用于获取数据片段。那么,我如何在片段中使用findViewById来获取加速度传感器数据呢?非常感谢您的回答:)
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.hardware.SensorEventListener;
public class GetDataFragment extends Fragment implements SensorEventListener {
//definisi variabel-variabel
//Sensor accelerometer;
private Activity parent;
TextView koordinatX;
TextView koordinatY;
TextView koordinatZ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.get_data, container, false);
}
private SensorManager sensorManager;
private Sensor sensor;
//TextView koordinatX;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
parent = getActivity();
sensorManager = (SensorManager) parent.getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
koordinatX = (TextView)findViewById(R.id.coordinatx);
koordinatY = (TextView)findViewById(R.id.coordinaty);
koordinatZ = (TextView)findViewById(R.id.coordinatz);
}