Everything is ok in your code two changes you have to do in your code
First do this in onCreateView
myCalendar = Calendar.getInstance();
And add this
_date.show();
before
return _date;
Вопрос
I am trying to open calender on click of edit text inside the Fragment.But when i am clicking on the the edit text it is throwing null pointer exception to me. Same code is working fine in the Activity Class but not working fine in the Fargement .
Exception
03-05 19:02:39.888: E/AndroidRuntime(1092): FATAL EXCEPTION: main
03-05 19:02:39.888: E/AndroidRuntime(1092): java.lang.NullPointerException
03-05 19:02:39.888: E/AndroidRuntime(1092): at com.test.newapp.AppTrip.showDialog(AppTrip.java:75)
03-05 19:02:39.888: E/AndroidRuntime(1092): at com.test.newapp.AppTrip$2.onClick(AppTrip.java:54)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.view.View.performClick(View.java:4212)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.view.View$PerformClick.run(View.java:17476)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.os.Handler.handleCallback(Handler.java:800)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.os.Handler.dispatchMessage(Handler.java:100)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.os.Looper.loop(Looper.java:194)
03-05 19:02:39.888: E/AndroidRuntime(1092): at android.app.ActivityThread.main(ActivityThread.java:5371)
03-05 19:02:39.888: E/AndroidRuntime(1092): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 19:02:39.888: E/AndroidRuntime(1092): at java.lang.reflect.Method.invoke(Method.java:525)
03-05 19:02:39.888: E/AndroidRuntime(1092): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-05 19:02:39.888: E/AndroidRuntime(1092): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-05 19:02:39.888: E/AndroidRuntime(1092): at dalvik.system.NativeStart.main(Native Method)
Fragment Code
public class AppTrip extends Fragment {
View rootView;
EditText from_date, to_date;
DatePickerDialog.OnDateSetListener date;
static final int DATE_DIALOG_ID = 999;
Calendar myCalendar;
int val;
Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
rootView = inflater.inflate(R.layout.main, container, false);
from_date = (EditText) rootView.findViewById(R.id.from_date);
to_date = (EditText) rootView.findViewById(R.id.to_date);
from_date.setFocusableInTouchMode(false);
to_date.setFocusableInTouchMode(false);
date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel(val);
}
};
from_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
val = 1;
}
});
to_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
val = 2;
}
});
return rootView;
}
protected Dialog showDialog(int dateDialogId) {
final Calendar now = Calendar.getInstance();
switch (dateDialogId) {
case DATE_DIALOG_ID:
DatePickerDialog _date = new DatePickerDialog(context, date,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)) {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
if (year - now.get(Calendar.YEAR) > 1) {
view.updateDate(year - 1,
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
if (year - now.get(Calendar.YEAR) == 1
&& monthOfYear > now.get(Calendar.MONTH)) {
view.updateDate(year, myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
if (year - now.get(Calendar.YEAR) == 1
&& monthOfYear == now.get(Calendar.MONTH)
&& dayOfMonth > now.get(Calendar.DAY_OF_MONTH)) {
view.updateDate(year, myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
if (year < now.get(Calendar.YEAR))
view.updateDate(myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
if (monthOfYear < now.get(Calendar.MONTH)
&& year == now.get(Calendar.YEAR))
view.updateDate(myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
if (dayOfMonth < now.get(Calendar.DAY_OF_MONTH)
&& year == now.get(Calendar.YEAR)
&& monthOfYear == now.get(Calendar.MONTH))
view.updateDate(myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
}
};
return _date;
}
return null;
}
private void updateLabel(int val) {
String myFormat = "dd/MM/yy"; // In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
Log.d("Date value ", "===" + sdf.format(myCalendar.getTime()));
if (val == 1)
from_date.setText(sdf.format(myCalendar.getTime()));
else
to_date.setText(sdf.format(myCalendar.getTime()));
}
}
Решение 2
Everything is ok in your code two changes you have to do in your code
First do this in onCreateView
myCalendar = Calendar.getInstance();
And add this
_date.show();
before
return _date;
Другие советы
Change from
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH))
to
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH))
in showDialog() method.
You forgot to initialize Calendar myCalendar; on onCreateView()
myCalendar = Calendar.getInstance();
You are using the instance of myCalendar which isn't initialized. Try using now instead of myCalendar.
Inside fragments showdialog() method doesn't work. You can do that, setonclicklistener on from_date and call DatePickerDialog inside it. Do the other things same as you do in your activity class.. Hopefully that help you.