gpt4 book ai didi

java - 从 Java 到 Scala : SensorManager

转载 作者:行者123 更新时间:2023-11-30 04:06:28 28 4
gpt4 key购买 nike

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)

}
}

错误是 Eclipe 状态:

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 无法找出从哪个类调用方法?我该如何解决?

最佳答案

“mAccListener(_)”创建了一个类型为 android.view.View => Unit 的函数。但是没有版本的 registerListener 具有这样的功能。

Java 版本正在传递 SensorListener。 Scala 版本不应该做同样的事情吗?

您可能正在考虑从 View => Unit 到 SensorListener 的隐式转换...如果是这样,您需要自己构建它。

关于java - 从 Java 到 Scala : SensorManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11567873/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com