我试图制作一个片段,它可以带走一些东西:
日期
重量
高度
注释
现在我遇到了datePicker。 我设法让对话框出现,但当我选择一个日期时,它强制关闭应用程序。 我希望它采取我选择的日期并在此标签中显示
:dd / mm / yy
试图期待其他问题,但没有人帮助过我。 希望你能:) 这是碎片:
public class NewTracker extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.newtracker, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("מעקב חדש");
EditText dateSelect = (EditText)getView().findViewById(R.id.dateSelect);
dateSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
}
});
}
}
这是datepicker片段:
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
EditText edittext = (EditText) getView().findViewById(R.id.dateSelect);
String myFormat = "dd/MM/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
edittext.setText(sdf.format(c.getTime()));
}
}
这是错误日志。
Error:
04-26 15:12:47.022 14245-14245/com.example.matancohen.sg50 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.matancohen.sg50, PID: 14245
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.matancohen.sg50.DatePickerFragment.onDateSet(DatePickerFragment.java:40)
at android.app.DatePickerDialog.onClick(DatePickerDialog.java:171)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:175)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
ahmad的解决方案后的错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.matancohen.sg50, PID: 23173
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.matancohen.sg50.NewTracker.<init>(NewTracker.java:43)
at com.example.matancohen.sg50.Tracking$1.onClick(Tracking.java:41)
at android.view.View.performClick(View.java:6207)
at android.view.View$PerformClick.run(View.java:23639)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
当我选择日期后单击“确定”时,它会给我一个强制崩溃 我能做什么? 谢谢!
答案 0 :(得分:0)
您可以尝试以下两种方式中的任何一种:
public class DatePickerFragment extends DialogFragment
类实现.. NewTracker
将您的代码修改为:
public class NewTracker extends Fragment {
public static EditText dateSelect;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newtracker, container, false);
dateSelect = (EditText) v.findViewById(R.id.dateSelect);
dateSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
}
});
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("מעקב חדש");
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
String myFormat = "dd/MM/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
dateSelect.setText(sdf.format(c.getTime()));
}
}
}
interface
中使用DialogFragment
,在选择日期后通知来电fragment
。