2010-12-05 99 views
1

我想從一個警報對話框啓動日期選擇器。但由於某些原因,showDialog(DATE_DIALOG_ID)根本無法打開。有人可以告訴我什麼是錯的。從警報對話框的android日期選擇器

final AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
     alt_bld.setIcon(R.drawable.icon); 
     alt_bld.setTitle("Categories"); 
     alt_bld.setSingleChoiceItems(categoryType, -1, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 
       System.out.println("Category selected= "+categoryType[item]); 
       Toast.makeText(getApplicationContext(), "Category selected= "+categoryType[item], Toast.LENGTH_SHORT).show(); 
       category=categoryType[item]; 


       if(category.equalsIgnoreCase("Time")){ 
        showDialog(DATE_DIALOG_ID); 
        System.out.println("selected date here:"+selectedDate); 
        Intent intent = new Intent(MainActivity.this, ListViewActivity.class); 
        bundle.putString("category", category); 
        bundle.putString("attribute", selectedDate); 
        intent.putExtras(bundle); 
        startActivity(intent); 

       } 
其他

日期選擇器相關的代碼

static final int DATE_DIALOG_ID = 0; 



@Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DATE_DIALOG_ID: 
      return new DatePickerDialog(this, 
         mDateSetListener, 
         mYear, mMonth, mDay); 
     } 
     return null; 
    } 


    private DatePickerDialog.OnDateSetListener mDateSetListener = 
     new DatePickerDialog.OnDateSetListener() { 


     public void onDateSet(DatePicker view, int year, 
           int monthOfYear, int dayOfMonth) { 
      mYear = year; 
      mMonth = monthOfYear; 
      mDay = dayOfMonth; 
      selectedDate = Integer.toString(mMonth)+"-"+Integer.toString(mDay)+"-"+Integer.toString(mYear); 
      System.out.println("selected date is:"+selectedDate); 
     } 


    }; 

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.homescreen); 

     final Calendar cal = Calendar.getInstance(); 
     mYear = cal.get(Calendar.YEAR); 
     mMonth = cal.get(Calendar.MONTH); 
     mDay = cal.get(Calendar.DAY_OF_MONTH); 

回答

1

在你的代碼u的調用的ShowDialog(INT);並立即啓動新的意圖。你期望看到一個對話。事實上,會因爲對話沒有關閉而導致內存泄漏。

+0

嘿夥計..謝謝..那麼工作..放置意圖後來... :) – 2010-12-05 18:35:26