DateTimeDialogFragmentを設定できないようです。これは、次の場所でクラッシュし続けるためです。
datePicker = (DatePicker) getView().findViewById(R.id.DatePicker);
エラーログ:
02-14 21:38:43.262: E/AndroidRuntime(2435): FATAL EXCEPTION: main
02-14 21:38:43.262: E/AndroidRuntime(2435): java.lang.NullPointerException
02-14 21:38:43.262: E/AndroidRuntime(2435): at com.test.DateTimeDialogFragment.onActivityCreated(DateTimeDialogFragment.java:98)
02-14 21:38:43.262: E/AndroidRuntime(2435): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:847)
02-14 21:38:43.262: E/AndroidRuntime(2435): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
02-14 21:38:43.262: E/AndroidRuntime(2435): at android.app.BackStackRecord.run(BackStackRecord.java:622)
02-14 21:38:43.262: E/AndroidRuntime(2435): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
02-14 21:38:43.262: E/AndroidRuntime(2435): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
02-14 21:38:43.262: E/AndroidRuntime(2435): at android.os.Handler.handleCallback(Handler.java:605)
02-14 21:38:43.262: E/AndroidRuntime(2435): at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 21:38:43.262: E/AndroidRuntime(2435): at android.os.Looper.loop(Looper.java:137)
02-14 21:38:43.262: E/AndroidRuntime(2435): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-14 21:38:43.262: E/AndroidRuntime(2435): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 21:38:43.262: E/AndroidRuntime(2435): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 21:38:43.262: E/AndroidRuntime(2435): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-14 21:38:43.262: E/AndroidRuntime(2435): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-14 21:38:43.262: E/AndroidRuntime(2435): at dalvik.system.NativeStart.main(Native Method)
完全なコードは次のとおりです。
public class DateTimeDialogFragment extends DialogFragment implements
OnDateChangedListener, OnTimeChangedListener {
// Define constants for date-time picker.
public final int DATE_PICKER = 1;
public final int TIME_PICKER = 2;
public final int DATE_TIME_PICKER = 3;
// DatePicker reference
private DatePicker datePicker;
// TimePicker reference
private TimePicker timePicker;
// Calendar reference
private Calendar mCalendar;
// Define activity
private Activity activity;
// Define Dialog type
private int intDialogType;
// Constructor start
public DateTimeDialogFragment(Activity activity) {
this.activity = activity;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
// Get the layout inflater
LayoutInflater inflater = activity.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.date_time_dialog, null));
// Set title of dialog
builder.setMessage("Set Date")
// Set Ok button
.setPositiveButton("Set",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User ok the dialog
}
})
// Set Cancel button
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
})
// Set Neutral button (reset Time / Date)
.setNeutralButton("Reset",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// User reset the dialog
// reset();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// Grab a Calendar instance
mCalendar = Calendar.getInstance();
// Init date picker
datePicker = (DatePicker) getView().findViewById(R.id.DatePicker);
datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), this);
// Init time picker
timePicker = (TimePicker) getView().findViewById(R.id.TimePicker);
timePicker.setOnTimeChangedListener(this);
}
public void setIntDialogType(int intDialogType) {
this.intDialogType = intDialogType;
}
// Convenience wrapper for internal Calendar instance
public int get(final int field) {
return mCalendar.get(field);
}
// Reset DatePicker, TimePicker and internal Calendar instance
public void reset() {
final Calendar c = Calendar.getInstance();
updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
updateTime(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
}
// Convenience wrapper for internal Calendar instance
public long getDateTimeMillis() {
return mCalendar.getTimeInMillis();
}
// Convenience wrapper for internal TimePicker instance
public void setIs24HourView(boolean is24HourView) {
timePicker.setIs24HourView(is24HourView);
}
// Convenience wrapper for internal TimePicker instance
public boolean is24HourView() {
return timePicker.is24HourView();
}
// Convenience wrapper for internal DatePicker instance
public void setCalendarViewShown(boolean calendarView) {
datePicker.setCalendarViewShown(calendarView);
}
// Convenience wrapper for internal DatePicker instance
public boolean CalendarViewShown() {
return datePicker.getCalendarViewShown();
}
// Convenience wrapper for internal DatePicker instance
public void updateDate(int year, int monthOfYear, int dayOfMonth) {
datePicker.updateDate(year, monthOfYear, dayOfMonth);
}
// Convenience wrapper for internal TimePicker instance
public void updateTime(int currentHour, int currentMinute) {
timePicker.setCurrentHour(currentHour);
timePicker.setCurrentMinute(currentMinute);
}
public String getDateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
return sdf.format(mCalendar.getTime());
}
// Called every time the user changes DatePicker values
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Update the internal Calendar instance
mCalendar.set(year, monthOfYear, dayOfMonth, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE));
}
// Called every time the user changes TimePicker values
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// Update the internal Calendar instance
mCalendar.set(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
}
date_time_dialog.xmlは次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DateTimePicker"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dip" >
<DatePicker
android:id="@+id/DatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="40dp" />
<TimePicker
android:id="@+id/TimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>