TimePicker出現,但是當我嘗試選擇時間。它引發了我下面的例外。java.lang.NullPointerException while設置時間
TimeFragment類。
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):at com.common.TimePickerFragment.onTimeSet(TimePickerFragment.java:47) 07-02 17:28:04.214:E/XXX(10341):at android.app.TimePickerDialog.tryNotifyTimeSet(Tim ePickerDialog.java:130) 07-02 17:28:04.214:E/XXX(10341):at android.app.TimePickerDialog.onClick(TimePickerDialog.java:115)07-02 17:28:04.214:E/XXX(10341):at com.android.internal.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:166) 07-02 17:28:04.214:E/XXX(10341):at android.os .Handler.dispatchMessage(Handler.java:99)07-02 17:28:04.214:E/XXX(10341):at android.os.Looper.loop(Looper.java:137)07-02 17:28 :04.214: E/XXX(10341):at android.app.ActivityThread.main(ActivityThread.java:4928)07-02 17:28:04.214:E/XXX(10341):at java.lang。反映。方法.invokeNative(本地方法)07-02 E/XXX(10341):at(0334):17/28 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:791) 07-02 17:28:04.214:E/XXX(10341):at com.android.internal.os.ZygoteInit。主(ZygoteInit.java:558)07-02 17:28:04.214:E/XXX(10341):在dalvik.system.NativeStart.main(本地 法)
嗨,「時間」是如何定義的?請點擊此處查看此鏈接:http://developer.android.com/reference/java/text/DateFormat.html#format(java.util.Date,java.lang.StringBuffer,java.text.FieldPosition)。格式()函數有三種不同的格式,我認爲你沒有引用它們。 – g00dy