我一直在尝试OnLongClickListener
在我的 Android 应用程序中使用 a 。我正在尝试OnLongClickListener
在用户按下按钮时使用 来增加一个数字。我采用了OnClickListener
在我的应用程序其他地方工作的代码,只是更新了它,OnLongClickListener
但是当我尝试运行我的应用程序运行时错误时发生。这是我的代码:
public void onCreate(Bundle savedInstanceState) //performed when application is first run
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //link to activity_main.xml
calculateElapsedTime();
Button myButton = (Button)findViewById(R.id.delayUp);
myButton.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v){
System.out.println("Hello World");
return true;
}
});
}
谁能提供一些有关此代码为何不起作用的见解。请注意,我在代码中有“Hello World”,这完全是为了测试目的,但是因为当应用程序加载时我得到运行时错误,显然这行代码永远不会被处理。
这是来自 LogCat 的错误:
09-16 17:22:34.982: D/dalvikvm(275): GC_EXTERNAL_ALLOC freed 750 objects / 55632 bytes in 179ms
09-16 17:22:35.113: D/AndroidRuntime(275): Shutting down VM
09-16 17:22:35.124: W/dalvikvm(275): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-16 17:22:35.203: E/AndroidRuntime(275): FATAL EXCEPTION: main
09-16 17:22:35.203: E/AndroidRuntime(275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.morinnic.intervaltimer/com.morinnic.intervaltimer.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton
09-16 17:22:35.203: E/AndroidRuntime(275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-16 17:22:35.203: E/AndroidRuntime(275): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-16 17:22:35.203: E/AndroidRuntime(275): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-16 17:22:35.203: E/AndroidRuntime(275): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-16 17:22:35.203: E/AndroidRuntime(275): at android.os.Handler.dispatchMessage(Handler.java:99)
09-16 17:22:35.203: E/AndroidRuntime(275): at android.os.Looper.loop(Looper.java:123)
09-16 17:22:35.203: E/AndroidRuntime(275): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-16 17:22:35.203: E/AndroidRuntime(275): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 17:22:35.203: E/AndroidRuntime(275): at java.lang.reflect.Method.invoke(Method.java:521)
09-16 17:22:35.203: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-16 17:22:35.203: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-16 17:22:35.203: E/AndroidRuntime(275): at dalvik.system.NativeStart.main(Native Method)
09-16 17:22:35.203: E/AndroidRuntime(275): Caused by: java.lang.ClassCastException: android.widget.ImageButton
09-16 17:22:35.203: E/AndroidRuntime(275): at com.morinnic.intervaltimer.MainActivity.onCreate(MainActivity.java:32)
09-16 17:22:35.203: E/AndroidRuntime(275): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-16 17:22:35.203: E/AndroidRuntime(275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-16 17:22:35.203: E/AndroidRuntime(275): ... 11 more
任何帮助将不胜感激。谢谢。