1

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

4

2 回答 2

1

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

Java 版本传入一个 SensorListener。Scala版本不应该做同样的事情吗?

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

于 2012-07-19T20:44:31.760 回答
0

在这些更正之后它可以工作:

class HelloAndroid extends Activity {

    def mAccListener() :SensorListener = new SensorListener {
        def onSensorChanged(x1: Int, x2: Array[Float]) = {
            var t=findViewById(R.id.acc).asInstanceOf[TextView]
            var x = x2(0).toString
            t.setText(x)
        }

        def onAccuracyChanged(a: Int, b: Int) = {
          //TODO
        }
    }

    override def onCreate(savedInstanceState : Bundle) {

    //...            

        mSensorManager.registerListener(mAccListener, SensorManager.SENSOR_DELAY_GAME)

  }
}

但我不明白为什么在 Java 和 Scala 中相同的函数采用不同的值。它既不方便又令人困惑:

+-------------------+------------------------------+---------------------------+
| Api Function      | Java                         | Scala                     |
+-------------------+------------------------------+---------------------------+
| onSensorChanged   | SensorEvent event            | x1: Int, x2: Array[Float] |
| onAccuracyChanged | Sensor sensor, int accuracy  | a: Int, b: Int            |
+-------------------+------------------------------+---------------------------+

有人可以解释一下吗?

于 2012-07-19T21:47:45.293 回答