class 或预期界面

class or interface expected

我曾经使用带有 android sdk 的 eclipse,但我刚刚下载了 android studio。我想使用加速度计,但由于某种原因,我一直遇到同样的问题。"Class or Interface expected"我附上下面的代码,如果有人能给我解决问题的方法,我将不胜感激。

   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);
}

}

由于错误 "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) {

您不小心关闭了 class 太早了,多了一个“}”:

   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!
Ctrl + Shift + numpad -

在 Android Studio 中执行此操作,这将折叠所有代码块。您可以轻松找到额外的 '}'