-5

I am getting NULLPointer Exception when I load the class offline_day. I have tried to solve the problem but I am unable to get at the root cause that why the NULLPointer Exception is occurring. I have edited my code after some useful advice. But still I am getting NPEeception. My class is something like this: Offline_day.java:-

    public class Offline_day extends DialogFragment implements OnDateChangedListener, OnTimeChangedListener
{
    public final static int DATE_PICKER = 1;
    public final static int TIME_PICKER = 2;
    public final static int DATE_TIME_PICKER = 3;
    private DatePicker datePicker;
    private TimePicker timePicker1;
    private TimePicker timePicker2;
    private Calendar mCalendar;
    private Activity activity;
    private int DialogType;
    private View mView;

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{
    //Activity activity;
    View mView = inflater.inflate(R.layout.offline_day, container, false);
    mCalendar = Calendar.getInstance();
    datePicker = (DatePicker) mView.findViewById(R.id.datePicker1);
   // datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), this);
    timePicker1 = (TimePicker) mView.findViewById(R.id.timePicker1);
    timePicker2 = (TimePicker) mView.findViewById(R.id.timePicker2);
    button=(Button)mView.findViewById(R.id.button1);
    //setIs24HourView(true);
    //setCalendarViewShown(false);
    //onDateChanged(datePicker,2015,6,5);
    //onTimeChanged(timePicker1,9,00);
    //onTimeChanged(timePicker2,19,30);        
    //datePicker.setVisibility(View.GONE);
    //timePicker1.setVisibility(View.GONE);
    //timePicker2.setVisibility(View.GONE);     
    return mView;
} 
@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) 
        return true;
    return super.onOptionsItemSelected(item);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    //timePicker1.setOnTimeChangedListener(this);
    //timePicker2.setOnTimeChangedListener(this);
}
/*public int get(final int field) 
{
    return mCalendar.get(field);
}
public long getDateTimeMillis() 
{
    return mCalendar.getTimeInMillis();
}
public void setIs24HourView(boolean is24HourView)
{
    timePicker1.setIs24HourView(is24HourView1());
    timePicker2.setIs24HourView(is24HourView2());
}
public boolean is24HourView1() 
{
    return timePicker1.is24HourView();
}
public boolean is24HourView2() 
{
    return timePicker2.is24HourView();
}
public void setCalendarViewShown(boolean calendarView) 
{
    datePicker.setCalendarViewShown(calendarView);
}
public boolean CalendarViewShown() 
{
    return datePicker.getCalendarViewShown();
}
public void updateDate(int year, int monthOfYear, int dayOfMonth) 
{
    datePicker.updateDate(year, monthOfYear, dayOfMonth);
}
public void updateTime(int currentHour, int currentMinute) 
{
    timePicker1.setCurrentHour(currentHour);
    timePicker1.setCurrentMinute(currentMinute);
    timePicker2.setCurrentHour(currentHour);
    timePicker2.setCurrentMinute(currentMinute);
}
public String getDateTime() 
{
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.getDefault());
    return sdf.format(mCalendar.getTime());
}*/
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
    mCalendar.set(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
}
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
{
    //mCalendar.set(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
    mCalendar.set(mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE));
}
/*private DatePickerDialog.OnDateSetListener datePickerListener=new DatePickerDialog.OnDateSetListener()
{
    public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) 
    {}
};
DatePickerDialog dialog = new DatePickerDialog(getActivity(), datePickerListener, 2000, 1,1);   */
}

This is my class.

Now when I load the application I get the following errors:

    06-11 16:30:54.229: E/AndroidRuntime(1592): FATAL EXCEPTION: main
06-11 16:30:54.229: E/AndroidRuntime(1592): Process: com.example.mobidoot PID: 1592
06-11 16:30:54.229: E/AndroidRuntime(1592): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
06-11 16:30:54.229: E/AndroidRuntime(1592):     at android.app.DatePickerDialog.resolveDialogTheme(DatePickerDialog.java:88)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at android.app.DatePickerDialog.<init>(DatePickerDialog.java:105)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at android.app.DatePickerDialog.<init>(DatePickerDialog.java:82)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at android.view.View.performClick(View.java:4780)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at android.view.View$PerformClick.run(View.java:19866)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at android.os.Handler.handleCallback(Handler.java:739)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at android.os.Looper.loop(Looper.java:135)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at android.app.ActivityThread.main(ActivityThread.java:5257)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at java.lang.reflect.Method.invoke(Native Method)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at java.lang.reflect.Method.invoke(Method.java:372)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-11 16:30:54.229: E/AndroidRuntime(1592):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

I am still getting NPException. Any help is highly appreciated.

Laxmi Narayan
  • 1,043
  • 1
  • 7
  • 15
  • start listening the LINT hints ... android doesn't like Fragment derived classes with non default constructors ... so move `Offline_day(Activity activity, int DialogType)` to more proper method onSomething ... – Selvin Jun 10 '15 at 12:51

2 Answers2

3
DatePickerDialog dialog = new DatePickerDialog(getActivity(), datePickerListener, 2000, 1,1);   

getActivity() will return null when you're initializing your Fragment. You'll have to wait until onAttach() in the fragment lifecycle for it to return non-null.

That's the reason for the NPE.

Overall, don't have non-default constructors in fragments. Set up the fragment view only in onCreateView() for generic fragments, or onCreateDialog() for DialogFragments.

laalto
  • 137,703
  • 64
  • 254
  • 280
  • @Selvin Thanks, clarified – laalto Jun 10 '15 at 12:58
  • I'm currently using `onCreateView()` in my DialogFragments as well. No problems in the last 2 years... yet. Should I start worrying about? I use `onCreateDialog()` only to remove the title from the Dialog (since I use custom layouts). But I do the rest in `onCreateView()`, as I do in all other Fragments. – Phantômaxx Jun 10 '15 at 13:09
  • 1
    Hmmm actually I was wrong... After thinking about I get the conclusion that default OnCreateView returns view for dialog itself... But, but if you wana use Datapickerdialog you shouldn't change it as it has it own view defined... The whole code provided by op is wrong – Selvin Jun 10 '15 at 14:33
  • @laalto: I have edited my code . But still I am getting NPException. Can you please help me where I am going wrong. – Laxmi Narayan Jun 11 '15 at 05:36
0

Use getActivity() instated of getContext() in your fragment.

Blaztix
  • 1,034
  • 1
  • 15
  • 25
Abdurahman Popal
  • 1,562
  • 16
  • 15