2

嘿,我正在尝试制作一个 TimepickerDialog。但是,当我按下“设置时间”按钮时,应用程序立即崩溃。这是代码。

主要活动:

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

我是否缺少一些权限或任何东西?

这是所要求的logcat:

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

谢谢

4

3 回答 3

3

我认为您正在从您的 xml 布局文件 onClick 属性中调用 chooseTime() 您需要将方法签名更改为此

public void chooseTime(View view){

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

}
于 2013-04-15T12:46:25.817 回答
0

改变方法如

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

在你xml Control喜欢

 <ImageButton
        android:id="@+id/imagestart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="chooseTime"
        android:src="@drawable/ic_launcher" />
于 2013-04-15T12:53:56.247 回答
0

对于显示TimepickerDialog,无需添加任何类型的权限。

mainActivity.java

public class MainActivity extends Activity {
     Button b1;
     static final int TIME_OPENDIALOG_ID = 0;
     private int mHour,mMinute;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1=(Button)findViewById(R.id.b1);
        b1.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                showDialog(TIME_OPENDIALOG_ID);
                updateDisplay();
            }

        });
    }
    private TimePickerDialog.OnTimeSetListener mopenSetListener = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mHour = hourOfDay;
            mMinute = minute;
            updateDisplay();
        }
    };
    private Object pad(int mMinute2) {
        if (mMinute2 >= 10)
            return String.valueOf(mMinute2);
        else
            return "0" + String.valueOf(mMinute2);
    }
    private void updateDisplay() {
        b1.setText(new StringBuilder().append(pad(mHour)).append(":")
                .append(pad(mMinute)));

    }
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_OPENDIALOG_ID:
            return new TimePickerDialog(this, mopenSetListener, mHour, mMinute,
                    false);

        }
        return null;
    }
}

主要的.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp"
        android:text="Time" />

</RelativeLayout>
于 2013-04-15T13:01:20.420 回答