我曾经使用eclipse和android sdk,但我刚刚下载了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) {
答案 0 :(得分:7)
你不小心用一个额外的&#34;}&#34;:
关闭了你的课程 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!
答案 1 :(得分:0)
Ctrl + Shift + numpad -
在 Android Studio 中执行此操作,这将折叠所有代码块。您可以轻松找到额外的“}”