{"id":2561,"date":"2022-08-30T15:25:53","date_gmt":"2022-08-30T15:25:53","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/03\/android-java-time-picker-dialog-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:25:53","modified_gmt":"2022-08-30T15:25:53","slug":"android-java-time-picker-dialog-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/android-java-time-picker-dialog-collection-of-common-programming-errors\/","title":{"rendered":"Android Java Time Picker Dialog-Collection of common programming errors"},"content":{"rendered":"<p>hey I am trying to make a TimepickerDialog. But the app crashes soon as I press the Set Time button. Here is the code.<\/p>\n<p>MainActivity:<\/p>\n<pre><code>package com.wifitimer;\nimport java.text.DateFormat;\nimport java.util.Calendar;\nimport android.app.Activity;\nimport android.app.TimePickerDialog;\nimport android.os.Bundle;\nimport android.widget.TimePicker;\n\npublic class MainActivity extends Activity {\nDateFormat formatDateTime=DateFormat.getDateTimeInstance();\nCalendar dateTime=Calendar.getInstance();\n\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_main);\n}\n\npublic void chooseTime(){\n    new TimePickerDialog(this, t, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), true).show();\n}\n\nTimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {\n    @Override\n    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {\n        dateTime.set(Calendar.HOUR_OF_DAY, hourOfDay);\n        dateTime.set(Calendar.MINUTE,minute);\n    }\n};\n}\n<\/code><\/pre>\n<p>Am I missing some permissions or anything?<\/p>\n<p>Here is the logcat as requested:<\/p>\n<pre><code>04-15 13:42:24.564: D\/libEGL(31887): loaded \/system\/lib\/egl\/libEGL_mali.so\n04-15 13:42:24.589: D\/libEGL(31887): loaded \/system\/lib\/egl\/libGLESv1_CM_mali.so\n04-15 13:42:24.594: D\/libEGL(31887): loaded \/system\/lib\/egl\/libGLESv2_mali.so\n04-15 13:42:24.599: D\/(31887): Device driver API match\n04-15 13:42:24.599: D\/(31887): Device driver API version: 10\n04-15 13:42:24.599: D\/(31887): User space API version: 10 \n04-15 13:42:24.599: D\/(31887): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 \n04-15 13:42:24.654: D\/OpenGLRenderer(31887): Enabling debug mode 0\n04-15 13:42:28.524: D\/AndroidRuntime(31887): Shutting down VM\n04-15 13:42:28.524: W\/dalvikvm(31887): threadid=1: thread exiting with uncaught exception (group=0x416922a0)\n04-15 13:42:28.544: E\/AndroidRuntime(31887): FATAL EXCEPTION: main\n04-15 13:42:28.544: E\/AndroidRuntime(31887): java.lang.IllegalStateException: Could not find a method chooseTime(View) in the activity class com.wifitimer.MainActivity for onClick handler on view class android.widget.Button with id 'timeBtn'\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at android.view.View$1.onClick(View.java:3678)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at android.view.View.performClick(View.java:4211)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at android.view.View$PerformClick.run(View.java:17267)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at android.os.Handler.handleCallback(Handler.java:615)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at android.os.Handler.dispatchMessage(Handler.java:92)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at android.os.Looper.loop(Looper.java:137)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at android.app.ActivityThread.main(ActivityThread.java:4898)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at java.lang.reflect.Method.invokeNative(Native Method)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at java.lang.reflect.Method.invoke(Method.java:511)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at dalvik.system.NativeStart.main(Native Method)\n04-15 13:42:28.544: E\/AndroidRuntime(31887): Caused by: java.lang.NoSuchMethodException: chooseTime [class android.view.View]\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at java.lang.Class.getConstructorOrMethod(Class.java:460)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at java.lang.Class.getMethod(Class.java:915)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    at android.view.View$1.onClick(View.java:3671)\n04-15 13:42:28.544: E\/AndroidRuntime(31887):    ... 11 more\n<\/code><\/pre>\n<p>Thanks<\/p>\n<ol>\n<li>\n<p>I think you are calling chooseTime() from your xml layout file onClick attribute you need to change method signature to this<\/p>\n<pre><code>public void chooseTime(View view){\n\n    new TimePickerDialog(this, t, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), true).show();\n\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>change method like<\/p>\n<pre><code>public void chooseTime(View view){\n    new TimePickerDialog(this, t, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), true).show();\n}\n<\/code><\/pre>\n<p>and in You <code>xml Control<\/code> Like<\/p>\n<pre><code> \n<\/code><\/pre>\n<\/li>\n<li>\n<pre><code>==========================mainActivity.java\npublic class MainActivity extends Activity {\n     Button b1;\n     static final int TIME_OPENDIALOG_ID = 0;\n     private int mHour,mMinute;\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.main);\n        b1=(Button)findViewById(R.id.b1);\n        b1.setOnClickListener(new OnClickListener()\n        {\n\n            @Override\n            public void onClick(View arg0) {\n                \/\/ TODO Auto-generated method stub\n                showDialog(TIME_OPENDIALOG_ID);\n                updateDisplay();\n            }\n\n        });\n    }\n    private TimePickerDialog.OnTimeSetListener mopenSetListener = new TimePickerDialog.OnTimeSetListener() {\n        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {\n            mHour = hourOfDay;\n            mMinute = minute;\n            updateDisplay();\n        }\n    };\n    private Object pad(int mMinute2) {\n        if (mMinute2 &gt;= 10)\n            return String.valueOf(mMinute2);\n        else\n            return \"0\" + String.valueOf(mMinute2);\n    }\n    private void updateDisplay() {\n        b1.setText(new StringBuilder().append(pad(mHour)).append(\":\")\n                .append(pad(mMinute)));\n\n    }\n    protected Dialog onCreateDialog(int id) {\n        switch (id) {\n        case TIME_OPENDIALOG_ID:\n            return new TimePickerDialog(this, mopenSetListener, mHour, mMinute,\n                    false);\n\n        }\n        return null;\n    }\n}\n======================main.xml\n\n\n    \n\n\n=======================\nTry This ode And For Show TimepickerDialog No Need To Add Any Type Of Permission.\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-02-03 02:28:15. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>hey I am trying to make a TimepickerDialog. But the app crashes soon as I press the Set Time button. Here is the code. MainActivity: package com.wifitimer; import java.text.DateFormat; import java.util.Calendar; import android.app.Activity; import android.app.TimePickerDialog; import android.os.Bundle; import android.widget.TimePicker; public class MainActivity extends Activity { DateFormat formatDateTime=DateFormat.getDateTimeInstance(); Calendar dateTime=Calendar.getInstance(); @Override public void onCreate(Bundle savedInstanceState) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2561","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2561"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2561\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}