这是我用来创建自定义日期选择器的代码:
public class ShoppingListDatePicker extends Activity{
// These variables will hold the date values later
private int startYear, startMonth, startDay, endYear, endMonth, endDay;
DatePicker dpStartDate, dpEndDate;
public void showDatePicker() {
// Inflate your custom layout containing 2 DatePickers
LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
View customView = inflater.inflate(R.layout.custom_date_picker, null);
// Define your date pickers
dpStartDate = (DatePicker) customView.findViewById(R.id.dpStartDate);
dpEndDate = (DatePicker) customView.findViewById (R.id.dpEndDate);
// Build the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView); // Set the view of the dialog to your custom layout
builder.setTitle("Select start and end date");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
startYear = dpStartDate.getYear();
startMonth = dpStartDate.getMonth();
startDay = dpStartDate.getDayOfMonth();
endYear = dpEndDate.getYear();
endMonth = dpEndDate.getMonth();
endDay = dpEndDate.getDayOfMonth();
dialog.dismiss();
}});
// Create and show the dialog
builder.create().show();
}
}
每当我从其他活动调用该方法时,我都会收到空指针错误:
抱怨Lauyout Inflater。我不知道为什么..... 我通过选择菜单上的按钮来调用方法:
case R.id.bShop:
ShoppingListDatePicker shopListDatePick = new ShoppingListDatePicker();
shopListDatePick.showDatePicker();
break;
FullActivity:
public class ShoppingListDatePicker extends Activity{
// These variables will hold the date values later
private int startYear, startMonth, startDay, endYear, endMonth, endDay;
DatePicker dpStartDate, dpEndDate;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_date_picker);
}
/**
* Displays the start and end date picker dialog
*/
public void showDatePicker() {
// Inflate your custom layout containing 2 DatePickers
// LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
//View customView = inflater.inflate(R.layout.custom_date_picker, null);
LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.custom_date_picker, null);
// Define your date pickers
dpStartDate = (DatePicker) customView.findViewById(R.id.dpStartDate);
dpEndDate = (DatePicker) customView.findViewById (R.id.dpEndDate);
// Build the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView); // Set the view of the dialog to your custom layout
builder.setTitle("Select start and end date");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
startYear = dpStartDate.getYear();
startMonth = dpStartDate.getMonth();
startDay = dpStartDate.getDayOfMonth();
endYear = dpEndDate.getYear();
endMonth = dpEndDate.getMonth();
endDay = dpEndDate.getDayOfMonth();
dialog.dismiss();
}});
// Create and show the dialog
builder.create().show();
}
}