【问题标题】:Datepicker not working in higher versions of androidDatepicker 在更高版本的 android 中不起作用
【发布时间】:2015-01-18 18:42:42
【问题描述】:

我正在学习 android 编程并开发了一个小型应用程序,其中包括同一屏幕上的 2 个日期选择器(开始日期和结束日期)。

它适用于 android 版本 2.3,但当我在 android 版本 4.4 中设置日期时崩溃。

下面是java代码sn-p。

公共类 WriteExcelActivity 扩展 Activity {

   private TextView startDateDisplay;
    private TextView endDateDisplay;
    private Button startPickDate;
    private Button endPickDate;
    private Calendar startDate;
    private Calendar endDate;
    static final int DATE_DIALOG_ID = 0;

    private TextView activeDateDisplay;
    private Calendar activeDate;


   @Override 
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_writeexcel); 

        /*  capture our View elements for the start date function   */
        startDateDisplay = (TextView) findViewById(R.id.fromdate);
        startPickDate = (Button) findViewById(R.id.imageButton1);

        startDate = Calendar.getInstance();

        /* add a click listener to the button   */
        startPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                showDateDialog(startDateDisplay, startDate);
            }
        });

     /* capture our View elements for the end date function */
        endDateDisplay = (TextView) findViewById(R.id.todate);
        endPickDate = (Button) findViewById(R.id.imageButton2);

        /* get the current date */
        endDate = Calendar.getInstance();

        /* add a click listener to the button   */
        endPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
           showDateDialog(endDateDisplay, endDate);
     }
        });

        /* display the current date (this method is below)  */

    updateDisplay(startDateDisplay, startDate);
    updateDisplay(endDateDisplay, endDate);
    }

    private void updateDisplay(TextView dateDisplay, Calendar date) {
        dateDisplay.setText(
                new StringBuilder()
                    // Month is 0 based so add 1
                    .append(date.get(Calendar.MONTH) + 1).append("-")
                    .append(date.get(Calendar.DAY_OF_MONTH)).append("-")
                    .append(date.get(Calendar.YEAR)).append(" "));
            }

    public void showDateDialog(TextView dateDisplay, Calendar date) {
        activeDateDisplay = dateDisplay;
        activeDate = date;
        showDialog(DATE_DIALOG_ID);
    }

    private OnDateSetListener dateSetListener = new OnDateSetListener() {
        @Override


        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            activeDate.set(Calendar.YEAR, year);
            activeDate.set(Calendar.MONTH, monthOfYear);
            activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateDisplay(activeDateDisplay, activeDate);
            unregisterDateDisplay();
        }
    };

    private void unregisterDateDisplay() {
        activeDateDisplay = null;
        activeDate = null;
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
        }
        return null;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
        switch (id) {
            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
                break;
        }
    }



@Override public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; }

如果有人能指出错误,不胜感激。谢谢

=============================================

xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/toDate"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >



  <TextView
         android:id="@+id/textbox_fromdate" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/txt_textbox_fromdate"
         android:layout_marginTop="22dp" />



 <TextView
          android:id="@+id/fromdate"
          android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/textbox_fromdate"
     android:ems="10"
   />

  <Button
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/fromdate"
    android:layout_toRightOf="@+id/fromdate"

    android:drawableLeft="@drawable/ic_datepicker"
     />

 <TextView
     android:id="@+id/textbox_todate" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/txt_textbox_todate"
       android:layout_below="@+id/fromdate" />


 <TextView
     android:id="@+id/todate"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/textbox_todate"
     />


  <Button
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/todate"
    android:layout_toRightOf="@+id/todate"

    android:drawableLeft="@drawable/ic_datepicker"
    android:contentDescription="" />




 <Button
     android:id="@+id/btnexcel"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/todate"
     android:onClick="onClickWriteExcelData"
     android:text="Write excel data" />



</RelativeLayout>

enter code here

【问题讨论】:

  • logcat 中的崩溃是什么?
  • 我正在将apk文件复制到手机上进行安装和运行。因此不确定在哪里可以看到 logcat 消息。该设备没有出现在android studio中..所以不得不这样做......
  • 如果没有 logcat 输出,您将很难解决这个问题。您是否尝试过通过命令行(adb)获取它?检查这个:developer.android.com/tools/help/logcat.html

标签: android


【解决方案1】:

使用这个,它应该可以工作。

public void onDateSet(android.widget.DatePicker view, int year,int monthOfYear, int dayOfMonth) 

【讨论】:

  • 谢谢钱德拉,但它仍然给出同样的错误。它说应用程序已停止工作
  • 你能把你的 xml 代码也贴出来,这样很容易弄清楚吗?
  • 不要调用 unregisterDateDisplay();方法,因为它使文本视图和日历对象无效。这就是它生成 NPE 的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2013-05-18
  • 2015-03-04
  • 1970-01-01
  • 2021-02-20
  • 2019-10-09
相关资源
最近更新 更多