TimePicker 出现,但是当我尝试选择时间时。它向我抛出了下面的异常。
时间片段类。
import java.util.Calendar;
import com.actionbarsherlock.app.SherlockDialogFragment;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.TimePicker;
public class TimePickerFragment extends SherlockDialogFragment implements TimePickerDialog.OnTimeSetListener
{
private TimePickedListener mListener;
static int hour;
static int minute;
static Context mContext;
public static TimePickerFragment newInstance(Context context, TimePickedListener listener, Calendar now)
{
TimePickerFragment dialog = new TimePickerFragment();
mContext = context;
hour = now.get(Calendar.HOUR_OF_DAY);
minute = now.get(Calendar.MINUTE);
return dialog;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
return new TimePickerDialog(mContext, this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
{
// when the time is selected, send it to the activity via its callback
// interface method
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
mListener.onTimePicked(c);
}
public static interface TimePickedListener
{
public void onTimePicked(Calendar time);
}
}
在主片段中选择时间
public void selectTime(final TextView lblTime, final int position) {
hideRight();
timeFrag = TimePickerFragment.newInstance(getActivity(), new TimePickedListener() {@
Override
public void onTimePicked(Calendar time) {
lblTime.setText(DateFormat.format("h:mm a", time));
}
}, now);
timeFrag.show(getActivity().getSupportFragmentManager(), "timePicker");
}
07-02 17:28:04.214: E/XXX(10341): 未捕获的异常是: 07-02 17:28:04.214: E/XXX(10341): java.lang.NullPointerException 07-02 17:28:04.214: E/XXX(10341): 在 com.common.TimePickerFragment.onTimeSet(TimePickerFragment.java:47) 07-02 17:28:04.214: E/XXX(10341): 在 android.app.TimePickerDialog.tryNotifyTimeSet(TimePickerDialog.java :130) 07-02 17:28:04.214: E/XXX(10341): 在 android.app.TimePickerDialog.onClick(TimePickerDialog.java:115) 07-02 17:28:04.214: E/XXX(10341):在 com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) 07-02 17:28:04.214: E/XXX(10341): 在 android.os.Handler.dispatchMessage(Handler.java: 99) 07-02 17:28:04.214: E/XXX(10341): 在 android.os.Looper.loop(Looper.java:137) 07-02 17:28:04.214: E/XXX(10341): 在android.app.ActivityThread。main(ActivityThread.java:4928) 07-02 17:28:04.214: E/XXX(10341): at java.lang.reflect.Method.invokeNative(Native Method) 07-02 17:28:04.214: E/XXX (10341): 在 java.lang.reflect.Method.invoke(Method.java:511) 07-02 17:28:04.214: E/XXX(10341): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller。运行(ZygoteInit.java:791)07-02 17:28:04.214:E/XXX(10341):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)07-02 17:28: 04.214:E/XXX(10341):在 dalvik.system.NativeStart.main(本机方法)运行(ZygoteInit.java:791)07-02 17:28:04.214:E/XXX(10341):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)07-02 17:28: 04.214:E/XXX(10341):在 dalvik.system.NativeStart.main(本机方法)运行(ZygoteInit.java:791)07-02 17:28:04.214:E/XXX(10341):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)07-02 17:28: 04.214:E/XXX(10341):在 dalvik.system.NativeStart.main(本机方法)