我正在构建一个打开日历对话框的应用程序。每次我按下这个按钮,应用程序就会崩溃。下面是我得到的控制台消息:
. lang。尝试在空对象引用上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'在com.example.mycontactlist.DatePickerDialog.onCreateView (DatePickerDialog.java: 41)androidx.fragment.app.Fragment.performCreateView (Fragment.java: 2600)androidx.fragment.app.FragmentManagerImpl.moveToState (FragmentManagerImpl.java: 881)androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState (FragmentManagerImpl.java: 1238)androidx.fragment.app.FragmentManagerImpl.moveToState (FragmentManagerImpl.java: 1303)androidx.fragment.app.BackStackRecord.executeOps (BackStackRecord.java: 439)androidx.fragment.app.FragmentManagerImpl.executeOps (FragmentManagerImpl.java: 2079)androidx.fragment.app.FragmentManagerImpl.executeOpsTogether (FragmentManagerImpl.java: 1869)androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (FragmentManagerImpl.java: 1824)androidx.fragment.app.FragmentManagerImpl.execPendingActions (FragmentManagerImpl.java: 1727)在androidx.fragment.app.FragmentManagerImpl 2.美元运行(FragmentManagerImpl.java: 150)android.os.Handler.handleCallback (Handler.java: 938)android.os.Handler.dispatchMessage (Handler.java: 99)android.os.Looper.loop (Looper.java: 223)android.app.ActivityThread.main (ActivityThread.java: 7656)在java.lang.reflect.Method。调用(本地方法)com.android.internal.os.RuntimeInit MethodAndArgsCaller.run美元(RuntimeInit.java: 592)com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 947)I/Process:发送信号。Pid: 9726 sig: 9与目标虚拟机断开连接,地址:'localhost:61100',传输:'socket'
下面是按钮
的代码`private void initChangeDateButton(){
Button changeDate = findViewById(R.id.btnBirthday);
changeDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
DatePickerDialog datePickerDialog = new DatePickerDialog();
datePickerDialog.show(fm,"DatePick");
}
});
下面是日期选择器对话框的代码
public class DatePickerDialog extends DialogFragment {
Calendar selectedDate;
public interface SaveDateListener{
void didFinishDatePickerDialog(Calendar selectedTime);
}
public DatePickerDialog(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
final View view = inflater.inflate(R.layout.select_date, container);
getDialog().setTitle("Select Date");
selectedDate = Calendar.getInstance(TimeZone.getDefault());
final CalendarView cv = view.findViewById(R.id.calendarView);
cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int day) {
selectedDate.set(year,month,day);
}
});
Button saveButton= view.findViewById(R.id.buttonSave);
saveButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
saveItem(selectedDate);
}
});
Button cancelButton= view.findViewById(R.id.buttonCancel);
saveButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
getDialog().dismiss();
}
});
return view;
}
private void saveItem(Calendar selectedTime){
SaveDateListener activity = (SaveDateListener) getActivity();
activity.didFinishDatePickerDialog(selectedTime);
getDialog().dismiss();
}
}
select_date布局LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="end">
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/buttonCancel"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/cancel"
android:textColor="@color/dialog_button_color"
/>
<Button
android:id="@+id/buttonSelect"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/select"
android:textColor="@color/dialog_button_color"
/>
</LinearLayout>
</LinearLayout>
感谢你的帮助
你的代码有
Button saveButton= view.findViewById(R.id.buttonSave);
但是你的XML有
<Button
android:id="@+id/buttonSelect"
注意id是如何不匹配的-这就是为什么你的saveButton
变量总是空的。您需要更改其中一个以使它们匹配(您选择哪一个并不重要,只要它们匹配即可)。