-2

i am so new to android programming

today i have written an application to access accelerometer from give online examples from here 1 & 2

i have completed my program but problem is application is not launching . i think there is a mistake in my program but cant find it

here is the code

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.hardware.SensorEventListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

import static android.content.Context.SENSOR_SERVICE;

public class MainActivity extends AppCompatActivity{
    public SensorManager msen;
    public Sensor sensor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        msen = (SensorManager) getSystemService(SENSOR_SERVICE);
        if (msen != null) {
            sensor=msen.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        }


    }
    protected void onResume() {
        super.onResume();
        msen.registerListener((SensorEventListener) this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }
    public final void onSensorChanged(SensorEvent event) {

        float x = event.values[0];
        float y=event.values[1];
        float z=event.values[2];
        float ans=x*x+y*y+z*z;
        ans= (float) Math.sqrt(ans);
        TextView value;
        value=(TextView) findViewById(R.id.val);
        value.setText((int) ans);
        value.setHeight(100);
        value.setWidth(100);
        value.setGravity(Gravity.CENTER);


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

output i get is

unfotunately speed has stopped

error log

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.jasinthpremkumar.speed, PID: 13210
                  java.lang.RuntimeException: Unable to resume activity {com.example.jasinthpremkumar.speed/com.example.jasinthpremkumar.speed.MainActivity}: java.lang.ClassCastException: com.example.jasinthpremkumar.speed.MainActivity cannot be cast to android.hardware.SensorEventListener
                      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2974)
                      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3005)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2371)
                      at android.app.ActivityThread.access$800(ActivityThread.java:149)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5290)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
                   Caused by: java.lang.ClassCastException: com.example.jasinthpremkumar.speed.MainActivity cannot be cast to android.hardware.SensorEventListener
                      at com.example.jasinthpremkumar.speed.MainActivity.onResume(MainActivity.java:32)
                      at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
                      at android.app.Activity.performResume(Activity.java:6106)
                      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2963)
                      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3005) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2371) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:149) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5290) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 
Application terminated.
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22

3 Answers3

2

SensorEventListener is an interface and You can not cast an Activity to SensorEventListener interface

Caused by: java.lang.ClassCastException: com.example.jasinthpremkumar.speed.MainActivity cannot be cast to android.hardware.SensorEventListener

You are getting above exception because You didn't implements SensorEventListener interface in your MainActivity and you are using msen.registerListener((SensorEventListener) this,sensor, SensorManager.SENSOR_DELAY_NORMAL); inisde onResume()

Try this You need to implements SensorEventListener interface in your MainActivity

Change your activity code like this

public class MainActivity extends AppCompatActivity implements SensorEventListener {


    public SensorManager msen;
    public Sensor sensor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        msen = (SensorManager) getSystemService(SENSOR_SERVICE);
        if (msen != null) {
            sensor=msen.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        }


    }
    protected void onResume() {
        super.onResume();
        msen.registerListener(this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }
    public final void onSensorChanged(SensorEvent event) {

        float x = event.values[0];
        float y=event.values[1];
        float z=event.values[2];
        float ans=x*x+y*y+z*z;
        ans= (float) Math.sqrt(ans);
        TextView value;
        value=(TextView) findViewById(R.id.val);
        value.setText((int) ans);
        value.setHeight(100);
        value.setWidth(100);
        value.setGravity(Gravity.CENTER);


    }

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

    }

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


}
Goku
  • 9,102
  • 8
  • 50
  • 81
  • it gives an error `error: MainActivity is not abstract and does not override abstract method onAccuracyChanged(Sensor,int) in SensorEventListener public class MainActivity extends AppCompatActivity implements SensorEventListener{` – jasinth premkumar Apr 20 '18 at 12:21
  • @jasinthpremkumar you need to Override this method in your activity `@Override public void onAccuracyChanged(Sensor sensor, int accuracy) { }` – Goku Apr 20 '18 at 12:22
  • @jasinthpremkumar check in above code i have Override that method – Goku Apr 20 '18 at 12:23
  • message is gone still not opening but in console it says E/SensorManager: Exception dispatching input event. – jasinth premkumar Apr 20 '18 at 12:26
2

Error Says

MainActivity cannot be cast to android.hardware.SensorEventListener

That means you did no implement MainActivity with SenserEventListener. And error comes from line below.

msen.registerListener((SensorEventListener) this,sensor, SensorManager.SENSOR_DELAY_NORMAL);

Here you are casting MainActivity to SenserEventListener while there is no relationship between these two . These should be a IS-A relationship. So solution is implements SenserEventListener in MainActivity and override the abstract methods.

public class MainActivity extends AppCompatActivity implements SenserEventListener

Then you can use directly with this.

msen.registerListener(this,sensor, SensorManager.SENSOR_DELAY_NORMAL);
ADM
  • 20,406
  • 11
  • 52
  • 83
1

You need to implement SensorEventListener to your activity like below :

public class MainActivity extends AppCompatActivity implements SensorEventListener{

      ----- your code ----- 
      ----- your code -----
}

That's all you need to do. Hope it would help.

Ankit Mehta
  • 4,251
  • 4
  • 19
  • 27