Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class or interface expected

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) {
like image 950
safiulm Avatar asked Mar 11 '15 04:03

safiulm


People also ask

What does .class expected mean?

class' expected. public class Calculator{ public Calculator(){ } public int sum(int one, int two) { int s = one + two; return int s; } } This error usually means that you are trying to declare or specify a variable type inside of return statement or inside of a method calls.

What is .class error in Java?

public class Error extends Throwable. An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.


1 Answers

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!
like image 141
FoggyDay Avatar answered Sep 20 '22 14:09

FoggyDay