Scala/Android 新手问题。我正在尝试将这个简单的代码从 Java 重写为 Scala。Java工作代码:
private final SensorEventListener mAccListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
// ... some code
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.start).setOnClickListener(this);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mAccSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(mAccListener, mAccSensor,
SensorManager.SENSOR_DELAY_GAME);
}
...和斯卡拉:
package com.example.hello
import android.app.Activity
import android.content.Context
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorManager
import android.os.Bundle
import android.os.Bundle
import android.view.View.OnClickListener
import android.view.View
import android.view.View
import android.widget.Toast
import android.widget.Toast
import android.hardware.SensorEventListener
class HelloAndroid extends Activity {
def mAccListener (v: View) {
// ... some code
}
override def onCreate(savedInstanceState : Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
def onClick(v: View) {
Toast.makeText(v.getContext, "Hello World", Toast.LENGTH_LONG).show()
}
})
val mSensorManager = getSystemService(Context.SENSOR_SERVICE).asInstanceOf[SensorManager]
val mAccSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
mSensorManager.registerListener(mAccListener(_), mAccSensor,
SensorManager.SENSOR_DELAY_GAME)
}
}
错误是 Eclip 状态:
Multiple markers at this line
- missing arguments for method mAccListener in class HelloAndroid; follow this method with `_' if you want to treat it as a partially applied function
- overloaded method value registerListener with alternatives: (android.hardware.SensorEventListener,android.hardware.Sensor,Int,android.os.Handler)Boolean <and>
(android.hardware.SensorEventListener,android.hardware.Sensor,Int)Boolean <and> (android.hardware.SensorListener,Int,Int)Boolean <and> (android.hardware.SensorListener,Int)Boolean
cannot be applied to (android.view.View => Unit, android.hardware.Sensor, Int)
据我了解,Scala 找不到从哪个类调用方法?我该如何解决?