本文介绍了Android的Java的时间选择器对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哎,我试图让TimepickerDialog。但应用程序崩溃,很快我preSS的时间设置按钮。这里是code。

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:

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) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void chooseTime(){
    new TimePickerDialog(this, t, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), true).show();
}

TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        dateTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
        dateTime.set(Calendar.MINUTE,minute);
    }
};
}

我缺少一些权限或什么?

Am I missing some permissions or anything?

下面是要求logcat的:

Here is the logcat as requested:

04-15 13:42:24.564: D/libEGL(31887): loaded /system/lib/egl/libEGL_mali.so
04-15 13:42:24.589: D/libEGL(31887): loaded /system/lib/egl/libGLESv1_CM_mali.so
04-15 13:42:24.594: D/libEGL(31887): loaded /system/lib/egl/libGLESv2_mali.so
04-15 13:42:24.599: D/(31887): Device driver API match
04-15 13:42:24.599: D/(31887): Device driver API version: 10
04-15 13:42:24.599: D/(31887): User space API version: 10 
04-15 13:42:24.599: D/(31887): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
04-15 13:42:24.654: D/OpenGLRenderer(31887): Enabling debug mode 0
04-15 13:42:28.524: D/AndroidRuntime(31887): Shutting down VM
04-15 13:42:28.524: W/dalvikvm(31887): threadid=1: thread exiting with uncaught exception (group=0x416922a0)
04-15 13:42:28.544: E/AndroidRuntime(31887): FATAL EXCEPTION: main
04-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'
04-15 13:42:28.544: E/AndroidRuntime(31887):    at android.view.View$1.onClick(View.java:3678)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at android.view.View.performClick(View.java:4211)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at android.view.View$PerformClick.run(View.java:17267)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at android.os.Handler.handleCallback(Handler.java:615)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at android.os.Looper.loop(Looper.java:137)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at android.app.ActivityThread.main(ActivityThread.java:4898)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at java.lang.reflect.Method.invokeNative(Native Method)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at java.lang.reflect.Method.invoke(Method.java:511)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at dalvik.system.NativeStart.main(Native Method)
04-15 13:42:28.544: E/AndroidRuntime(31887): Caused by: java.lang.NoSuchMethodException: chooseTime [class android.view.View]
04-15 13:42:28.544: E/AndroidRuntime(31887):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at java.lang.Class.getMethod(Class.java:915)
04-15 13:42:28.544: E/AndroidRuntime(31887):    at android.view.View$1.onClick(View.java:3671)
04-15 13:42:28.544: E/AndroidRuntime(31887):    ... 11 more

感谢

推荐答案

我觉得你是从你的XML布局文件的onClick调用chooseTime()的属性,你需要改变方法签名本

I think you are calling chooseTime() from your xml layout file onClick attribute you need to change method signature to this

public void chooseTime(View view){

    new TimePickerDialog(this, t, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), true).show();

}

这篇关于Android的Java的时间选择器对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 01:53