6

I used to use eclipse with android sdk but I just downloaded android studio. I wanted to use the accelerometer but for some reason I keep getting the same issue."Class or Interface expected" I am attaching the code below,if anyone can give me a solution to the problem, I will be obliged.

   package com.example.symbox.myapplication;

   import android.app.Activity;
   import android.content.Context;
   import android.hardware.Sensor;
   import android.hardware.SensorEvent;
   import android.hardware.SensorEventListener;
   import android.hardware.SensorManager;
   import android.support.v7.app.ActionBarActivity;
   import android.os.Bundle;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.view.View;
   import android.widget.ImageView;
   import android.widget.TextView;


   public class MainActivity extends ActionBarActivity implements SensorEventListener {

   private float mLastX, mLastY, mLastZ;

   private boolean mInitialized;
   private SensorManager mSensorManager;
   private Sensor mAccelerometer;
   private final float NOISE = (float) 2.0;

   @Override
   public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mInitialized = false;

    mSensorManager = (SensorManager) getSystemService(Context.SEARCH_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
}




 protected void onResume(){

    super.onResume();

    mSensorManager.registerListener(this,mAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);

}
  protected void onPause(){

super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor,int accuracy){

}

public void onSensorChanged(SensorEvent event){
TextView tvX= (TextView)findViewById(R.id.x_axis);
TextView tvY= (TextView)findViewById(R.id.y_axis);
TextView tvZ= (TextView)findViewById(R.id.z_axis);

ImageView iv = (ImageView)findViewById(R.id.image);

float x = event.values[0];
float y = event.values[1];
float z = event.values[2];

if(!mInitialized){
    mLastX = x;
    mLastY = y;
    mLastZ = z ;

    tvX.setText("0.0");
    tvY.setText("0.0");
    tvZ.setText("0.0");

    mInitialized = true;
} else {
    float deltaX = Math.abs(mLastX - x);
    float deltaY = Math.abs(mLastY - y);
    float deltaZ = Math.abs(mLastZ - z);
    if (deltaX < NOISE) deltaX = (float)0.0;
    if (deltaY < NOISE) deltaY = (float)0.0;
    if (deltaZ < NOISE) deltaZ = (float)0.0;
    mLastX = x;
    mLastY = y;
    mLastZ = z;
    tvX.setText(Float.toString(deltaX));
    tvY.setText(Float.toString(deltaY));
    tvZ.setText(Float.toString(deltaZ));
    iv.setVisibility(View.VISIBLE);
    if (deltaX > deltaY) {
        iv.setImageResource(R.drawable.horizontal);
    } else if (deltaY > deltaX) {
        iv.setImageResource(R.drawable.vertical);
    } else {
        iv.setVisibility(View.INVISIBLE);
    }
  }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

These are the logs that I keep on seeing due to the error "Class or Interface expected"

C:\Users\Abeer\AndroidStudioProjects\test_one\app\src\main\java\com\example\symbox\test_one\MainActivity.java:38: error: class, interface, or enum expected
    public boolean onCreateOptionsMenu(Menu menu) {
           ^
C:\Users\Abeer\AndroidStudioProjects\test_one\app\src\main\java\com\example\symbox\test_one\MainActivity.java:41: error: class, interface, or enum expected
        return true;
        ^
C:\Users\Abeer\AndroidStudioProjects\test_one\app\src\main\java\com\example\symbox\test_one\MainActivity.java:42: error: class, interface, or enum expected
    }
    ^
C:\Users\Abeer\AndroidStudioProjects\test_one\app\src\main\java\com\example\symbox\test_one\MainActivity.java:45: error: class, interface, or enum expected
    public boolean onOptionsItemSelected(MenuItem item) {
           ^
C:\Users\Abeer\AndroidStudioProjects\test_one\app\src\main\java\com\example\symbox\test_one\MainActivity.java:52: error: class, interface, or enum expected
        if (id == R.id.action_settings) {
        ^
C:\Users\Abeer\AndroidStudioProjects\test_one\app\src\main\java\com\example\symbox\test_one\MainActivity.java:54: error: class, interface, or enum expected
        }
        ^
C:\Users\Abeer\AndroidStudioProjects\test_one\app\src\main\java\com\example\symbox\test_one\MainActivity.java:57: error: class, interface, or enum expected
    }
    ^
C:\Users\Abeer\AndroidStudioProjects\test_one\app\src\main\java\com\example\symbox\test_one\MainActivity.java:60: error: class, interface, or enum expected
    public void onSensorChanged(SensorEvent event) {
           ^
C:\Users\Abeer\AndroidStudioProjects\test_one\app\src\main\java\com\example\symbox\test_one\MainActivity.java:65: error: class, interface, or enum expected
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
Kaushik
  • 6,150
  • 5
  • 39
  • 54
safiulm
  • 85
  • 1
  • 1
  • 5

2 Answers2

12

You accidentally closed your class too soon, with an extra "}":

   public class MainActivity extends ActionBarActivity implements SensorEventListener {

    private float mLastX, mLastY, mLastZ;
    ...
    mSensorManager = (SensorManager) getSystemService(Context.SEARCH_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
}  // Delete this line!
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • Thank you it worked but now I have another issue. the app was successfully built but when I ran it on my phone (cannot test the accelerometer on my laptop) I get the error "Unfortunately, My application has stopped". Any thoughts? – safiulm Mar 11 '15 at 19:47
  • 2
    Q: Any thoughts? A: Yes: look at your logs, step through the program on the debugger. I use Eclipse; it sounds like you're using Android Studio. Either way, you can 1) attach the debugger to your handset, and 2) review any logs generated on your handset. Look here for some very useful tips: http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – FoggyDay Mar 11 '15 at 21:15
0
Ctrl + Shift + numpad -

In Android Studio do this, this will collapse all the code block. And you can easily find your extra '}'

Surajkumar_cse
  • 165
  • 1
  • 11