设置时间时java.lang.NullPointerException

时间:2013-07-02 12:02:35

标签: java android

TimePicker出现,但是当我尝试选择时间时。它抛出了我的例外情况。

TimeFragment Class。

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(TimePickerDialog.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.reflect.Method.invokeNative(Native Method)07-02   17:28:04.214:E / XXX(10341):at   java.lang.reflect.Method.invoke(Method.java:511)07-02 17:28:04.214:   E / XXX(10341):at   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.main(ZygoteInit.java:558)07-02   17:28:04.214:E / XXX(10341):at dalvik.system.NativeStart.main(Native   方法)

2 个答案:

答案 0 :(得分:6)

您忘了初始化private TimePickedListener mListener;

使用以下内容更改newInstance方法

public static TimePickerFragment newInstance(Context context, TimePickedListener listener, Calendar now)
    {
        TimePickerFragment dialog = new TimePickerFragment();
        mContext = context;
        mListener = listener;
        hour = now.get(Calendar.HOUR_OF_DAY);
        minute = now.get(Calendar.MINUTE);

        return dialog;
    }

答案 1 :(得分:3)

您没有在任何地方实例化private TimePickedListener mListener;。它引用了null

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);
    mListener = listener; // you missed this line
    return dialog;
}