我正在尝试在android中实现一个日期选择器。日期选择器应该在点击按钮后出现。是的,我知道这很简单,但是作为一个初学者,我有点卡住了。下面是与任务相关的代码部分。
public class NeedFragment extends Fragment {
private DatePicker datePicker;
private Calendar calendar;
private TextView dateView;
private int year, month, day;
Fragment fragment;
String selectedDate=null;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_need_blood, container, false);
Button button1 = (Button)rootView.findViewById(R.id.dialog_btn1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().showDialog(999);
if(selectedDate != null) {
Log.d("date",selectedDate);
}
//openAppointments(dialog);
}
});
return rootView;
}
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (id == 999) {
return new DatePickerDialog(getActivity().getApplicationContext()
, myDateListener, year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
// arg1 = year
// arg2 = month
// arg3 = day
StringBuilder sb=null;
sb.append(arg3).append("/")
.append(arg2+1).append("/").append(arg1);
returnDate(sb.toString());
}
};
public void returnDate(String s)
{
selectedDate=s;
}
}
这是错误:
06-25 15:19:53.150: E/AndroidRuntime(13160): FATAL EXCEPTION: main
06-25 15:19:53.150: E/AndroidRuntime(13160): Process: com.bloodbank.slidingmenu, PID: 13160
06-25 15:19:53.150: E/AndroidRuntime(13160): java.lang.NullPointerException: println needs a message
06-25 15:19:53.150: E/AndroidRuntime(13160): at android.util.Log.println_native_inner(Native Method)
06-25 15:19:53.150: E/AndroidRuntime(13160): at android.util.Log.println_native(Log.java:290)
06-25 15:19:53.150: E/AndroidRuntime(13160): at android.util.Log.d(Log.java:323)
06-25 15:19:53.150: E/AndroidRuntime(13160): at com.bloodbank.slidingmenu.NeedBloodFragment$3.onClick(NeedBloodFragment.java:220)
06-25 15:19:53.150: E/AndroidRuntime(13160): at android.view.View.performClick(View.java:4761)
06-25 15:19:53.150: E/AndroidRuntime(13160): at android.view.View$PerformClick.run(View.java:19767)
06-25 15:19:53.150: E/AndroidRuntime(13160): at android.os.Handler.handleCallback(Handler.java:739)
06-25 15:19:53.150: E/AndroidRuntime(13160): at android.os.Handler.dispatchMessage(Handler.java:95)
06-25 15:19:53.150: E/AndroidRuntime(13160): at android.os.Looper.loop(Looper.java:135)
06-25 15:19:53.150: E/AndroidRuntime(13160): at android.app.ActivityThread.main(ActivityThread.java:5312)
06-25 15:19:53.150: E/AndroidRuntime(13160): at java.lang.reflect.Method.invoke(Native Method)
06-25 15:19:53.150: E/AndroidRuntime(13160): at java.lang.reflect.Method.invoke(Method.java:372)
06-25 15:19:53.150: E/AndroidRuntime(13160): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
06-25 15:19:53.150: E/AndroidRuntime(13160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
编辑:以前的错误已解决。我还有一个问题。我可以开始一个活动后,我点击确定在日期对话框?
5条答案
按热度按时间r1wp621o1#
一旦更改以下行
到
1qczuiv02#
删除此行
并在返回日期内添加这一行
1l5u6lss3#
试试这个代码
3lxsmp7m4#
在主Activity中尝试这段代码,然后在布局中使用正确的id名称创建一个按钮和文本视图并运行它,您可以在http://www.ahotbrew.com/android-datepicker-example/上找到完整的博客文章及其源代码
mzillmmw5#
步骤2.活动_主文件. xml
访问此https://solutioninpocket.com/posts/android-datepicker-example