我知道另一个主题就是用这种方式调用,但它没有回答我的问题。
Eclipse强调setOnClickListener
指向
`loc.setOnClickListener(new View.OnClickListener()
并说
令牌“setOnClickListener”上的语法错误,=此令牌后的预期值。
我开始练习Android,我认为解决方案非常简单。提前谢谢!
package com.example.vll;
import com.example.vll.R;
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.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.lang.Math;
public class MainActivity extends Activity {
private SensorManager mySensorManager;
private Sensor myGravitySensor;
private Sensor myAccelerometerSensor;
TextView x_position_value;
TextView y_position_value;
TextView z_position_value;
TextView x_normal_vector_value;
TextView y_normal_vector_value;
TextView z_normal_vector_value;
TextView textLightSensor;
private float xPositionValue=0;
private float yPositionValue=0;
private float zPositionValue=0;
private float xAccelerometerValue=0;
private float yAccelerometerValue=0;
private float zAccelerometerValue=0;
private float xGravityValue=0;
private float yGravityValue=0;
private float zGravityValue=0;
Button loc;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loc = (Button) findViewById(R.id.localize);
x_position_value = (TextView) findViewById(R.id.x_position_value);
y_position_value = (TextView) findViewById(R.id.y_position_value);
z_position_value = (TextView) findViewById(R.id.z_position_value);
mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
myGravitySensor = mySensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
myAccelerometerSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (myAccelerometerSensor == null){
textLightSensor.setText("No Accelerometer Sensor!");
}else{
//mySensorManager.registerListener(GravitySensorEventListener, myGravitySensor, SensorManager.SENSOR_DELAY_FASTEST);
mySensorManager.registerListener(AccelerometerSensorEventListener, myAccelerometerSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
}
SensorEventListener AccelerometerSensorEventListener
= new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
xAccelerometerValue=event.values[0];
yAccelerometerValue=event.values[1];
zAccelerometerValue=event.values[2];
}
}};
/*
SensorEventListener GravitySensorEventListener
= new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_GRAVITY){
xGravityValue=event.values[0];
yGravityValue=event.values[1];
zGravityValue=event.values[2];
}
}
};
*/
//MY PROBLEM IS HERE
loc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
x_normal_vector_value.setText(" x = "+String.valueOf(xAccelerometerValue*Math.pow(Math.pow(Math.pow(xAccelerometerValue,2)+Math.pow(yAccelerometerValue,2)+Math.pow(zAccelerometerValue,2), 0.5), -1)));
y_normal_vector_value.setText(" y = "+String.valueOf(yAccelerometerValue*Math.pow(Math.pow(Math.pow(xAccelerometerValue,2)+Math.pow(yAccelerometerValue,2)+Math.pow(zAccelerometerValue,2), 0.5), -1)));
z_normal_vector_value.setText(" z = "+String.valueOf(zAccelerometerValue*Math.pow(Math.pow(Math.pow(xAccelerometerValue,2)+Math.pow(yAccelerometerValue,2)+Math.pow(zAccelerometerValue,2), 0.5), -1)));
}
});
}
答案 0 :(得分:2)
这是一个错位}
的案例。正确检查它们并使用IDE不应该那么困难
您需要更改此
mySensorManager.registerListener(AccelerometerSensorEventListener,myAccelerometerSensor, SensorManager.SENSOR_DELAY_FASTEST);
} // else end here
} // onCreate brace end here.
到
mySensorManager.registerListener(AccelerometerSensorEventListener, myAccelerometerSensor, SensorManager.SENSOR_DELAY_FASTEST);
} // else end here
最后有支柱}
});
} // oncreate
} // activity
答案 1 :(得分:1)
您需要确保onClickListener
部分位于onCreate
方法范围内。
这是正确的代码:
package com.example.vll;
import com.example.vll.R;
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.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.lang.Math;
public class MainActivity extends Activity {
private SensorManager mySensorManager;
private Sensor myGravitySensor;
private Sensor myAccelerometerSensor;
TextView x_position_value;
TextView y_position_value;
TextView z_position_value;
TextView x_normal_vector_value;
TextView y_normal_vector_value;
TextView z_normal_vector_value;
TextView textLightSensor;
private float xPositionValue=0;
private float yPositionValue=0;
private float zPositionValue=0;
private float xAccelerometerValue=0;
private float yAccelerometerValue=0;
private float zAccelerometerValue=0;
private float xGravityValue=0;
private float yGravityValue=0;
private float zGravityValue=0;
Button loc;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loc = (Button) findViewById(R.id.localize);
x_position_value = (TextView) findViewById(R.id.x_position_value);
y_position_value = (TextView) findViewById(R.id.y_position_value);
z_position_value = (TextView) findViewById(R.id.z_position_value);
mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
myGravitySensor = mySensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
myAccelerometerSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (myAccelerometerSensor == null){
textLightSensor.setText("No Accelerometer Sensor!");
}else{
//mySensorManager.registerListener(GravitySensorEventListener, myGravitySensor, SensorManager.SENSOR_DELAY_FASTEST);
mySensorManager.registerListener(AccelerometerSensorEventListener, myAccelerometerSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
//MY PROBLEM IS HERE
loc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
x_normal_vector_value.setText(" x = "+String.valueOf(xAccelerometerValue*Math.pow(Math.pow(Math.pow(xAccelerometerValue,2)+Math.pow(yAccelerometerValue,2)+Math.pow(zAccelerometerValue,2), 0.5), -1)));
y_normal_vector_value.setText(" y = "+String.valueOf(yAccelerometerValue*Math.pow(Math.pow(Math.pow(xAccelerometerValue,2)+Math.pow(yAccelerometerValue,2)+Math.pow(zAccelerometerValue,2), 0.5), -1)));
z_normal_vector_value.setText(" z = "+String.valueOf(zAccelerometerValue*Math.pow(Math.pow(Math.pow(xAccelerometerValue,2)+Math.pow(yAccelerometerValue,2)+Math.pow(zAccelerometerValue,2), 0.5), -1)));
}
});
}
SensorEventListener AccelerometerSensorEventListener
= new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
xAccelerometerValue=event.values[0];
yAccelerometerValue=event.values[1];
zAccelerometerValue=event.values[2];
}
}};
/*
SensorEventListener GravitySensorEventListener
= new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_GRAVITY){
xGravityValue=event.values[0];
yGravityValue=event.values[1];
zGravityValue=event.values[2];
}
}
};
*/
}
答案 2 :(得分:1)
问题只是第69行的额外}
,我刚删除它。您可以在项目中粘贴以下代码,它将正常工作,
public class MainActivity extends Activity {
private SensorManager mySensorManager;
private Sensor myGravitySensor;
private Sensor myAccelerometerSensor;
TextView x_position_value;
TextView y_position_value;
TextView z_position_value;
TextView x_normal_vector_value;
TextView y_normal_vector_value;
TextView z_normal_vector_value;
TextView textLightSensor;
private float xPositionValue=0;
private float yPositionValue=0;
private float zPositionValue=0;
private float xAccelerometerValue=0;
private float yAccelerometerValue=0;
private float zAccelerometerValue=0;
private float xGravityValue=0;
private float yGravityValue=0;
private float zGravityValue=0;
Button loc;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loc = (Button) findViewById(R.id.localize);
x_position_value = (TextView) findViewById(R.id.x_position_value);
y_position_value = (TextView) findViewById(R.id.y_position_value);
z_position_value = (TextView) findViewById(R.id.z_position_value);
mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
myGravitySensor = mySensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
myAccelerometerSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (myAccelerometerSensor == null){
textLightSensor.setText("No Accelerometer Sensor!");
}else{
//mySensorManager.registerListener(GravitySensorEventListener, myGravitySensor, SensorManager.SENSOR_DELAY_FASTEST);
mySensorManager.registerListener(AccelerometerSensorEventListener, myAccelerometerSensor, SensorManager.SENSOR_DELAY_FASTEST);
}
SensorEventListener AccelerometerSensorEventListener
= new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
xAccelerometerValue=event.values[0];
yAccelerometerValue=event.values[1];
zAccelerometerValue=event.values[2];
}
}};
/*
SensorEventListener GravitySensorEventListener
= new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_GRAVITY){
xGravityValue=event.values[0];
yGravityValue=event.values[1];
zGravityValue=event.values[2];
}
}
};
*/
//MY PROBLEM IS HERE
loc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
x_normal_vector_value.setText(" x = "+String.valueOf(xAccelerometerValue*Math.pow(Math.pow(Math.pow(xAccelerometerValue,2)+Math.pow(yAccelerometerValue,2)+Math.pow(zAccelerometerValue,2), 0.5), -1)));
y_normal_vector_value.setText(" y = "+String.valueOf(yAccelerometerValue*Math.pow(Math.pow(Math.pow(xAccelerometerValue,2)+Math.pow(yAccelerometerValue,2)+Math.pow(zAccelerometerValue,2), 0.5), -1)));
z_normal_vector_value.setText(" z = "+String.valueOf(zAccelerometerValue*Math.pow(Math.pow(Math.pow(xAccelerometerValue,2)+Math.pow(yAccelerometerValue,2)+Math.pow(zAccelerometerValue,2), 0.5), -1)));
}
});
}
}