web-dev-qa-db-ja.com

クラスまたはインターフェイスが必要です

以前はAndroid sdkでEclipseを使用していましたが、Android studioをダウンロードしました。加速度計を使用したかったのですが、何らかの理由で同じ問題が発生し続けます。 「クラスまたはインターフェースが必要です」以下のコードを添付しています。誰かが問題の解決策を教えてくれれば、私は義務付けられます。

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

}

これらは、「クラスまたはインターフェイスが必要です」というエラーが原因で表示され続けるログです。

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) {
5
safiulm

余分な「}」を付けて、誤ってクラスをすぐに閉じてしまいました。

   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!
8
FoggyDay