我从 Scala + Android 开始(并使用 sbt android 插件)。我试图在没有实现 View.OnClickListener 的活动的情况下将按钮操作连接到按钮。
由于找不到该方法,因此在运行时单击按钮失败。我正在处理的文档说,我只需要声明一个公共 void 方法,对操作采取一个视图,并在布局中使用该方法名称。
我做错了什么?
MainActivity.scala
package net.badgerhunt.hwa
import android.app.Activity
import android.os.Bundle
import android.widget.Button
import android.view.View
import java.util.Date
class MainActivity extends Activity {
override def onCreate(savedInstanceState: Bundle) = {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
}
def calculate(button: View): Unit = println("calculating with %s ...".format(button))
}
资源/布局/main.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button"
android:text=""
android:onClick="calculate"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
点击失败
D/AndroidRuntime( 362): Shutting down VM
W/dalvikvm( 362): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime( 362): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 362): java.lang.IllegalStateException: Could not find a method calculate(View) in the activity
E/AndroidRuntime( 362): at android.view.View$1.onClick(View.java:2020)
E/AndroidRuntime( 362): at android.view.View.performClick(View.java:2364)
E/AndroidRuntime( 362): at android.view.View.onTouchEvent(View.java:4179)
E/AndroidRuntime( 362): at android.widget.TextView.onTouchEvent(TextView.java:6540)
E/AndroidRuntime( 362): at android.view.View.dispatchTouchEvent(View.java:3709)
E/AndroidRuntime( 362): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
E/AndroidRuntime( 362): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
E/AndroidRuntime( 362): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
E/AndroidRuntime( 362): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
E/AndroidRuntime( 362): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
E/AndroidRuntime( 362): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
E/AndroidRuntime( 362): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
E/AndroidRuntime( 362): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
E/AndroidRuntime( 362): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 362): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 362): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 362): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 362): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 362): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 362): Caused by: java.lang.NoSuchMethodException: calculate
E/AndroidRuntime( 362): at java.lang.ClassCache.findMethodByName(ClassCache.java:308)
E/AndroidRuntime( 362): at java.lang.Class.getMethod(Class.java:1014)
E/AndroidRuntime( 362): at android.view.View$1.onClick(View.java:2017)
E/AndroidRuntime( 362): ... 20 more
更新
考虑到这可能是 sbt android 插件的错误,我加倍确定该方法在编译后存在。使用 javap ...
Compiled from "MainActivity.scala"
public class net.badgerhunt.hwa.MainActivity extends android.app.Activity implements scala.ScalaObject{
public net.badgerhunt.hwa.MainActivity();
public void calculate(android.view.View);
public void onCreate(android.os.Bundle);
public int $tag() throws java.rmi.RemoteException;
}