Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从android中的碎片读取传感器时出现问题_Java_Android_Android Fragments_Fragment_Sensors - Fatal编程技术网

Java 从android中的碎片读取传感器时出现问题

Java 从android中的碎片读取传感器时出现问题,java,android,android-fragments,fragment,sensors,Java,Android,Android Fragments,Fragment,Sensors,我在片段中显示和与传感器交互时遇到问题。我已经有两个工作碎片,但当我加载传感器碎片时,它会强制关闭 sensors.java是片段,看起来像这样 public class sensors extends Fragment implements SensorEventListener { private float lastX, lastY, lastZ; private SensorManager sensorManager; private Sensor accelerometer; pri

我在片段中显示和与传感器交互时遇到问题。我已经有两个工作碎片,但当我加载传感器碎片时,它会强制关闭

sensors.java是片段,看起来像这样

public class sensors extends Fragment implements SensorEventListener
{
private float lastX, lastY, lastZ;
private SensorManager sensorManager;
private Sensor accelerometer;

private float deltaXMax = 0;
private float deltaYMax = 0;
private float deltaZMax = 0;

private float deltaX = 0;
private float deltaY = 0;
private float deltaZ = 0;

private float vibrateThreshold = 0;

private TextView currentX, currentY, currentZ, maxX, maxY, maxZ;

public Vibrator v;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.sensor, container, false);
    initializeViews();

    sensorManager = (SensorManager) 
getActivity().getSystemService(Context.SENSOR_SERVICE);
    if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
        // success! we have an accelerometer

        accelerometer = 
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, accelerometer, 
SensorManager.SENSOR_DELAY_NORMAL);
        vibrateThreshold = accelerometer.getMaximumRange() / 2;
    } else {
        // fai! we dont have an accelerometer!
    }

    //initialize vibration
    v = (Vibrator) 
this.getActivity().getSystemService(Context.VIBRATOR_SERVICE);
    return rootView;
}

public void initializeViews() {
    currentX = (TextView) getView().findViewById(R.id.currentX);
    currentY = (TextView) getView().findViewById(R.id.currentY);
    currentZ = (TextView) getView().findViewById(R.id.currentZ);

    maxX = (TextView) getView().findViewById(R.id.maxX);
    maxY = (TextView) getView().findViewById(R.id.maxY);
    maxZ = (TextView) getView().findViewById(R.id.maxZ);
}

//onResume() register the accelerometer for listening the events
public void onResume() {
    super.onResume();
    sensorManager.registerListener(this, accelerometer, 
SensorManager.SENSOR_DELAY_NORMAL);
}

//onPause() unregister the accelerometer for stop listening the events
public void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

@Override
public void onSensorChanged(SensorEvent event) {

    // clean current values
    displayCleanValues();
    // display the current x,y,z accelerometer values
    displayCurrentValues();
    // display the max x,y,z accelerometer values
    displayMaxValues();

    // get the change of the x,y,z values of the accelerometer
    deltaX = Math.abs(lastX - event.values[0]);
    deltaY = Math.abs(lastY - event.values[1]);
    deltaZ = Math.abs(lastZ - event.values[2]);

    // if the change is below 2, it is just plain noise
    if (deltaX < 2)
        deltaX = 0;
    if (deltaY < 2)
        deltaY = 0;
    if (deltaZ < 2)
        deltaZ = 0;

    // set the last know values of x,y,z
    lastX = event.values[0];
    lastY = event.values[1];
    lastZ = event.values[2];

    vibrate();

}

// if the change in the accelerometer value is big enough, then vibrate!
// our threshold is MaxValue/2
public void vibrate() {
    if ((deltaX > vibrateThreshold) || (deltaY > vibrateThreshold) || 
(deltaZ > vibrateThreshold)) {
        v.vibrate(50);
    }
}

public void displayCleanValues() {
    currentX.setText("0.0");
    currentY.setText("0.0");
    currentZ.setText("0.0");
}

// display the current x,y,z accelerometer values
public void displayCurrentValues() {
    currentX.setText(Float.toString(deltaX));
    currentY.setText(Float.toString(deltaY));
    currentZ.setText(Float.toString(deltaZ));
}

// display the max x,y,z accelerometer values
public void displayMaxValues() {
    if (deltaX > deltaXMax) {
        deltaXMax = deltaX;
        maxX.setText(Float.toString(deltaXMax));
    }
    if (deltaY > deltaYMax) {
        deltaYMax = deltaY;
        maxY.setText(Float.toString(deltaYMax));
    }
    if (deltaZ > deltaZMax) {
        deltaZMax = deltaZ;
        maxZ.setText(Float.toString(deltaZMax));
    }
}




}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="center"
          android:background="#ffffff"
          android:gravity="center"
          android:orientation="vertical"
          android:paddingTop="20dp" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="10dp"
    android:text="Max Acceleration:"
    android:textSize="20dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:text="Max Acceleration: X-Axis"
    android:textSize="15dp" />

<TextView
    android:id="@+id/maxX"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
    android:text="0.0"
    android:textSize="20dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:text="Max Acceleration: Y-Axis"
    android:textSize="15dp" />

<TextView
    android:id="@+id/maxY"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
    android:text="0.0"
    android:textSize="20dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:text="Max Acceleration: Z-Axis"
    android:textSize="15dp" />

<TextView
    android:id="@+id/maxZ"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
    android:text="0.0"
    android:textSize="20dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="30dp"
    android:text="Current Acceleration:"
    android:textSize="20dp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:gravity="center|top"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.33"
        android:background="#ffffff"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:text="X-Axis"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/currentX"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:text="0.0"
            android:textSize="15dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.33"
        android:background="#ffffff"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:text="Y-Axis"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/currentY"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:text="0.0"
            android:textSize="15dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.33"
        android:background="#ffffff"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:text="Z-Axis"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/currentZ"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:text="0.0"
            android:textSize="15dp" />
    </LinearLayout>
</LinearLayout>

</RelativeLayout>

在设置根视图之前,在从
onCreateView()方法返回之前,调用
getView()

试试这个:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.sensor, container, false);
    initializeViews(rootView);
    ....
}
public void initializeViews(rootView) {
    currentX = (TextView) rootView.findViewById(R.id.currentX);
    currentY = (TextView) rootView.findViewById(R.id.currentY);
    currentZ = (TextView) rootView.findViewById(R.id.currentZ);

    maxX = (TextView) rootView.findViewById(R.id.maxX);
    maxY = (TextView) rootView.findViewById(R.id.maxY);
    maxZ = (TextView) rootView.findViewById(R.id.maxZ);
}

getView()
将返回null,直到
onCreateView()完成。如果您想在
onCreateView()
中初始化
视图
,您需要在
视图
上调用
findViewById()
。您可以将
View
参数添加到
initializeViews()
方法中,并将
rootView
传递给它。谢谢兄弟。有趣的是,我读到那条线,我想它一定是,它尝试了一百万种东西,甚至打破了它,最后迷失在酱汁中。如果其他人有这个问题,我只是把所有的东西移到充气机上,然后将rootView返回到OnViewCreated中。再次感谢@MikeM.Thank@Titus。我移动了除充气机以外的所有设备,并从OnCreateView返回到OnViewCreated。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.sensor, container, false);
    initializeViews(rootView);
    ....
}
public void initializeViews(rootView) {
    currentX = (TextView) rootView.findViewById(R.id.currentX);
    currentY = (TextView) rootView.findViewById(R.id.currentY);
    currentZ = (TextView) rootView.findViewById(R.id.currentZ);

    maxX = (TextView) rootView.findViewById(R.id.maxX);
    maxY = (TextView) rootView.findViewById(R.id.maxY);
    maxZ = (TextView) rootView.findViewById(R.id.maxZ);
}