2015-10-06 45 views
2

我一個新的Android和掙扎我怎麼能在一個單一的dialog box添加這兩個date pickertime picker請指導我如何能做到這一點我創建了一個dialog box和使用功能setview膨脹,但我得到了異常我怎樣才能把日期和時間選擇在對話框中的Android

  AlertDialog.Builder dialog = new AlertDialog.Builder(
        MainActivity.this); 
      dialog.setTitle("Begning"); 
      dialog.setPositiveButton("ok", 
        new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, 
           int which) { 
          // TODO Auto-generated method stub 

         } 
        }); 
      dialog.setNegativeButton("cancel", 
        new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, 
           int which) { 
          // TODO Auto-generated method stub 
          dialog.dismiss(); 
         } 
        }); 
      View pickers=getLayoutInflater().inflate(R.id.linearLayout1, 
      null); 
      dialog.setView(picker); 

      dialog.show(); 

     } 
    }); 

以上是我的代碼 和聽到的是xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<DatePicker 
    android:id="@+id/datePicker1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
</DatePicker> 

<TimePicker 
    android:id="@+id/timePicker1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
</TimePicker> 

enter image description here

我想創建這樣的事情,請幫我

10-06 15:49:24.261: E/Trace(9287): error opening trace file: No such file or directory (2) 
10-06 15:49:27.434: W/dalvikvm(9287): threadid=1: thread exiting with uncaught exception (group=0x411903a8) 
10-06 15:49:27.454: E/AndroidRuntime(9287): FATAL EXCEPTION: main 
10-06 15:49:27.454: E/AndroidRuntime(9287): android.content.res.Resources$NotFoundException: Resource ID #0x7f080000 type #0x12 is not valid 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2150) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at android.content.res.Resources.getLayout(Resources.java:860) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at android.view.LayoutInflater.inflate(LayoutInflater.java:394) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at com.example.datetimedialog.MainActivity$1.onClick(MainActivity.java:51) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at android.view.View.performClick(View.java:4102) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at android.view.View$PerformClick.run(View.java:17085) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at android.os.Handler.handleCallback(Handler.java:615) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at android.os.Handler.dispatchMessage(Handler.java:92) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at android.os.Looper.loop(Looper.java:155) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at android.app.ActivityThread.main(ActivityThread.java:5520) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at java.lang.reflect.Method.invoke(Method.java:511) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1058) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825) 
10-06 15:49:27.454: E/AndroidRuntime(9287):  at dalvik.system.NativeStart.main(Native Method) 

這是logcat我一邊跑

回答

0

我爲此在github上創建了一個項目。我採取了與您不同的方法,並通過擴展對話框類創建了帶日期和時間選擇器的自定義對話框。

https://github.com/ramesh130/DateTimePickerDialog

+0

三江源非常@ramesh不能告訴你我是多麼幸福的是,你真棒哥們,我已經serached幾乎每一頁,但沒有找到它 – ANinJa

+0

因爲你已經使用對話片段,它需要支持庫要運行API級別9,是否有其他方法,如果我們不想使用支持庫?請建議 – ANinJa

+0

您可以使用兼容性軟件包...但是代碼不取決於片段,您可以將其替換爲活動沒有任何問題... –

1

你應該使用R.layout.linearLayout1,不R.id.linearLayout1了,像這樣:

View pickers=getLayoutInflater().inflate(R.layout.linearLayout1,null); 

假設您的佈局文件的名稱是linearLayout1

相關問題