简体   繁体   English

OnLongClickListener导致运行时错误

[英]OnLongClickListener causes Run Time Errors

I have been attempting to use a OnLongClickListener in my Android app. 我一直在尝试在Android应用中使用OnLongClickListener I am trying to use the OnLongClickListener to increment a number while the user has a button pressed. 我试图在用户按下按钮时使用OnLongClickListener来增加数字。 I had taken OnClickListener code that is working elsewhere in my application and merely updated it OnLongClickListener however when I try to run my application runtime errors occur. 我采取了OnClickListener是在我的应用程序工作在其他地方的代码和仅仅更新它OnLongClickListener但是当我尝试运行我的应用程序运行时发生的错误。 Here is my code: 这是我的代码:

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;
        }
    });
}

Could anyone please provide some insight on why this code is not working. 任何人都可以提供一些有关为什么此代码无法正常工作的见解。 Note that I have "Hello World" in the code strictly for testing purposes however since I get run-time errors when the application loads obviously this line of code is never processed. 请注意,代码中严格包含“ Hello World”,仅用于测试目的,但是由于在应用程序加载时出现运行时错误,显然这行代码从未处理过。

Here is the errors from LogCat: 这是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

Any assistance would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

Given the information you give, the obvious possibility is: myButton might be null (which will throw a NullPointerException), that is, the element corresponding to the id does not exists on the view (yet), or it is not of the type Button (which will throw a ClassCastException). 给定您提供的信息,明显的可能性是:myButton可能为null(将引发NullPointerException),即,与id对应的元素在视图上不存在(尚未),或者它不是Button类型的(这将引发ClassCastException)。

Make sure that you call setContentView on the activity before your code above, and be sure that it really is a Button. 确保上面的代码之前在活动上调用setContentView ,并确保它确实是一个Button。

A sample from the output from your phone would help more. 手机输出的样本会有所帮助。 Find out how to use the adb logcat command, it will show the exact exception that you get. 了解如何使用adb logcat命令,它将显示您得到的确切异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM