Android studio direction sensor question

Asked 1 years ago, Updated 1 years ago, 43 views

import android.content.DialogInterface; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager;

import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView;

import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Set; import java.util.UUID;

public class MainActivity extends AppCompatActivity { private final int REQUEST_BLUETOOTH_ENABLE = 100;

 private TextView mConnectionStatus;
 private EditText mInputEditText;

 private TextView tv;
 private SensorManager sm;
 private Sensor s;
 private SensorEventListener sel;
 private float pitch;



 ConnectedTask mConnectedTask = null;
 static BluetoothAdapter mBluetoothAdapter;
 private String mConnectedDeviceName = null;
 private ArrayAdapter<String> mConversationArrayAdapter;
 static boolean isConnectionError = false;
 private static final String TAG = "BluetoothClient";



 @Override
 public void onCreate(Bundle savedInstanceState)
 {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     tv = (TextView)findViewById(R.id.textView1);
     sm = (SensorManager) getSystemService(SENSOR_SERVICE);
     s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

     Button go = (Button)findViewById(R.id.Button);
     go.setOnClickListener(new View.OnClickListener(){
         public void onClick(View v){
             String sendMessage = mInputEditText.getText().toString();
             switch (v.getId()) {
                 case R.id.Button:
                     sendMessage("m");
                     break;
             }
         }
     });

     Button stop = (Button)findViewById(R.id.Button2);
     stop.setOnClickListener(new View.OnClickListener(){
         public void onClick(View v){
             String sendMessage = mInputEditText.getText().toString();
     switch (v.getId()) {
         case R.id.Button2:
             sendMessage("s");
             break;
             }
         }

     });

     Button back = (Button)findViewById(R.id.Button3);
     back.setOnClickListener(new View.OnClickListener(){
         public void onClick(View v){
              String sendMessage = mInputEditText.getText().toString();
     switch (v.getId()) {
         case R.id.Button3:
         sendMessage("b");
         break;
             }
          }
     });

     mConnectionStatus = (TextView)findViewById(R.id.connection_status_textview);
     mInputEditText = (EditText)findViewById(R.id.input_string_edittext);
     ListView mMessageListview = (ListView) findViewById(R.id.message_listview);

     mConversationArrayAdapter = new ArrayAdapter<>( this,
             android.R.layout.simple_list_item_1 );
   mMessageListview.setAdapter(mConversationArrayAdapter);

 @Override
 protected void onResume() {
     super.onResume();

     sm.registerListener((SensorEventListener) this,s,SensorManager.SENSOR_DELAY_UI);
 }

 @Override
 protected void onPause() {
     super.onPause();
     sm.unregisterListener((SensorEventListener) this);
 }

 public  void onSensorChanged(SensorEvent event){
     if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
         String str = "Direction sensor value \n\n"
                 +"\nPitch:" +event.values[1];
         tv.setText(str);
     }
 }

 public void onAccuracyChanged(Sensor sensor, int accuracy){

 }

 protected void onDestroy() {
     super.onDestroy();

     if ( mConnectedTask != null ) {

         mConnectedTask.cancel(true);
     }
 }

I'm going to make an RC car that works when I connect to Raspberry Pie's eco-server through Bluetooth and send a proper text message. In the code to move forward and backward with the button to change direction using the tilt of the smartphone, I coded it like this to add a function that receives the slope sensor value before adding the direction change function.

The error appears this way

Caused by: java.lang.ClassCastException: com.example.practice.MainActivity cannot be cast to android.hardware.SensorEventListener at com.example.practice.MainActivity.onResume(MainActivity.java:141)

141 is -> sm.registerListener ((SensorEventListener) this,s,SensorManager.SENSOR_DELAY_UI);

What's the problem?

android-studio

2022-09-21 23:14

1 Answers

I think this is the main activity, but there is an error that it cannot be cast as a sensor event listener.

public class MainActivity extends AppCompatActivity implements SensorEventListener

Implement sensor event listener interface

Please put @Override on the on-sensor change and on-accuracy change methods


2022-09-21 23:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.