1

me da problema este código , me pone "foreach not applicable to type "List sensor "

introducir la descripción de la imagen aquí

> public class Sensores2Activity extends Activity 
>                                                                                 implements SensorEventListener {
> 
> private List<Sensor> listaSensores;
> 
> private TextView aTextView[][] = new TextView[20][3];
> 
>    
> 
> @Override public void onCreate(Bundle savedInstanceState) {
> 
>        super.onCreate(savedInstanceState);
> 
>        setContentView(R.layout.main);
> 
>        LinearLayout raiz = (LinearLayout) findViewById(R.id.raiz);
> 
>    SensorManager sm = (SensorManager)
> getSystemService(SENSOR_SERVICE);
> 
>        listaSensores = sm.getSensorList(Sensor.TYPE_ALL);
> 
>        int n = 0;
> 
>        for (Sensor sensor : listaSensores) {
> 
>              TextView mTextView = new TextView(this);
> 
>              mTextView.setText(sensor.getName());
> 
>              raiz.addView(mTextView);
> 
>              LinearLayout nLinearLayout = new LinearLayout(this);
> 
>              raiz.addView(nLinearLayout);
> 
>              for (int i = 0; i < 3; i++) {
> 
>                     aTextView[n][i] = new TextView(this);
> 
>                     aTextView[n][i].setText("?");
> 
>                     aTextView[n][i].setWidth(87);
> 
>              }
> 
>              TextView xTextView = new TextView(this);
> 
>              xTextView.setText("  X: ");
> 
>              nLinearLayout.addView(xTextView);
> 
>              nLinearLayout.addView(aTextView[n][0]);
> 
>              TextView yTextView = new TextView(this);
> 
>              yTextView.setText("  Y: ");
> 
>              nLinearLayout.addView(yTextView);
> 
>              nLinearLayout.addView(aTextView[n][1]);
> 
>              TextView zTextView = new TextView(this);
> 
>              zTextView.setText("  Z: ");
> 
>              nLinearLayout.addView(zTextView);
> 
>              nLinearLayout.addView(aTextView[n][2]);
> 
>              sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
> 
>              n++;
> 
>        }
> 
> }
> 
>  
> 
> @Override public void onAccuracyChanged(Sensor sensor, int accuracy)
> {}
> 
>  
> 
> @Override public void onSensorChanged(SensorEvent event) {
> 
>        synchronized (this) {
> 
>              int n = 0;
> 
>              for (Sensor sensor: listaSensores) {
> 
>                     if (event.sensor == sensor) {
> 
>                            for (int i=0; i<event.values.length; i++) {
> 
>                              aTextView[n][i].setText(Float.toString(event.values[i]));
> 
>                            }
> 
>                     }
> 
>                     n++;
> 
>              }
> 
>        }
> 
> }
> 
> }
0

1 respuesta 1

0

Al ver tu imagen me parece que el problema es simplemente que no estas agregando los "import" necesarios para hacer uso de las clasesen tu proyecto por esa razòn se marca en rojo Sensor y TextView, estos deberìas usar, agregalos arriba de la definiciòn de tu clase:

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;

public class Sensores2Activity extends Activity implements SensorEventListener {
...
...
1
  • Vale, no me da ningún error ya, una duda, habria alguna forma de comprobarlo en emulador, o solo se puede en dispostitivo?
    – alejandro
    el 28 abr. 2020 a las 0:44

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.