我正在尝试使用在 Internet 上找到的代码,但它根本没有运行。满足 bulid.gradle 条件并且代码没有问题。当我单击 Martial DatePicker 的按钮时,应用程序退出。
//梯度
implementation 'com.google.android.material:material:1.3.0'
//activity_add_plan
<Button
android:id="@+id/mdBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="material datepicker"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"/>
//添加PlanActivity
public class AddPlanActivity extends AppCompatActivity {
private TextView startD, finishD;
private Button mDatepickerBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_plan);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
startD = (TextView)findViewById(R.id.startDay);
finishD = (TextView)findViewById(R.id.finishDay);
mDatepickerBtn = findViewById(R.id.mdBtn);
//Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
//cal.clear();
//final long today = MaterialDatePicker.todayInUtcMilliseconds();
MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.datePicker();
builder.setTitleText("SELECT A DATE");
//builder.setSelection(today);
final MaterialDatePicker materialDatePicker = builder.build();
mDatepickerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
materialDatePicker.show(getSupportFragmentManager(), "DATE_PICKER");
}
});
materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
@Override
public void onPositiveButtonClick(Object selection) {
startD.setText(materialDatePicker.getHeaderText());
}
});
}
}