2012-04-27 28 views
6

Tôi đang tạo ứng dụng mà tôi đang cố gắng yêu cầu người dùng chọn thời gian, vì vậy tôi đã sử dụng datePickertimePickerbutton set. Vì vậy, khi anh ấy nhấp vào set button a dialog box đến và nói như bạn đã chọn x ngày và thời gian x hoặc theo bất kỳ cách nào một thông báo sẽ xuất hiện trên màn hình như người dùng đã chọn thời gian này. Vì vậy, tôi đã xây dựng mã nhưng khi ứng dụng của tôi đến hoạt động đó, nó luôn bị dừng mạnh. Không có vấn đề gì với xml file liên quan để hiển thị dữ liệu bởi vì khi tôi nhận xét dòng java tìm nạp giá trị từ datePickertimePicker, ứng dụng hoạt động hoàn toàn tốt . Tôi vẫn đang đăng cả hai mã tệp để dễ hiểu. cũng đăng ngoại lệ mèo đăng nhập. Tôi đã loại bỏ tất cả các mã không cần thiết như nhập khẩu và gói và nút thêm vv Để làm cho nó dễ đọc.Android: Cách lấy giá trị từ bộ chọn ngày và giờ

Đây là mã của .xml tập tin

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

<LinearLayout android:id="@+id/llheader" 
    android:layout_width="fill_parent" android:layout_height="20dp" 
    android:layout_weight="1" 
    android:background="@drawable/bg"> 

    <RelativeLayout android:id="@+id/relativeLayout1" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_gravity="center"> <!--header--> 
     <TextView 
      android:id="@+id/txt_header" 
      android:layout_width="wrap_content"   
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true"/>    
     </RelativeLayout>   
</LinearLayout> 
<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
<LinearLayout android:layout_width="wrap_content" 
    android:id="@+id/linearLayout1" android:orientation="vertical" 
    android:layout_height="wrap_content" 
<DatePicker android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/datePicker"></DatePicker> 
<TimePicker android:id="@+id/timePicker" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content</TimePicker> 
<TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content" 
    android:layout_width="match_parent" android:gravity="center"> 

    <Button android:id="@+id/btnSetDateTime" 
     android:layout_height="wrap_content" android:text="Set" 
     android:layout_width="wrap_content"/> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Next"      
     android:id="@+id/btnNext1" /> 
    </TableRow> 
</LinearLayout> 
</ScrollView>  
<LinearLayout android:id="@+id/lldata" 
    android:layout_weight="8" android:layout_width="fill_parent" 
    android:layout_height="0dp" android:background="#FFFFFF">   
</LinearLayout> 

<LinearLayout android:id="@+id/llfooter" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" android:layout_height="20dp" 
    android:visibility="visible" 
    android:layout_margin="0dp">  
</LinearLayout> 

Đây là mã của TimeDate.java:

public class TimeDate extends Activity 
{ 


Button btnNext; 
private TextView dateText; 
public void onCreate(Bundle savedInstanceState) 
{ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.time_date); 
     btnNext=(Button)this.findViewById(R.id.btnNext1);   
     btnNext.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 

        Intent myintent = new Intent(TimeDate.this, Next.class); 
        startActivity(myintent); 
        finish(); 
        } 
     }); 
     ScrollView DTPicker = (ScrollView) View.inflate(TimeDate.this,R.layout.time_date, null); 

     Button btnSet = (Button) DTPicker.findViewById(R.id.btnSetDateTime); 
     final DatePicker dp = (DatePicker) DTPicker.findViewById(R.id.datePicker); 
     final TimePicker tp = (TimePicker) DTPicker.findViewById(R.id.timePicker); 

     btnSet.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       String strDateTime = dp.getYear() + "-" + (dp.getMonth() + 1) + "-" + dp.getDayOfMonth() + " "+ tp.getCurrentHour() + ":" + tp.getCurrentMinute();}}); 

     AlertDialog alert = new AlertDialog.Builder(TimeDate.this).create(); 
     alert.setTitle("Reminder"); 
     alert.setView(DTPicker); 
     alert.show(); 

}  
} 

Đây là logcat ngoại lệ:

04-27 11:48:24.830: D/AndroidRuntime(812): Shutting down VM 
04-27 11:48:24.830: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
04-27 11:48:24.851: E/AndroidRuntime(812): FATAL EXCEPTION: main 
04-27 11:48:24.851: E/AndroidRuntime(812): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ogtpl.android/com.ogtpl.android.TimeDate}: java.lang.ClassCastException: android.widget.LinearLayout 
+1

có cùng một vấn đề như thế nào để có được những giá trị này .. xin vui lòng một ai đó trả lời tôi sẽ bỏ phiếu cáu kỉnh cho anh/cô ấy :) –

Trả lời

11

Đừng làm gì thêm, Mã của bạn chỉ là hoàn hảo. Chỉ cần xóa mã của ScrollLayout hoặc thuộc tính của nó ở bất cứ nơi nào bạn đang sử dụng. Lemme viết mã cập nhật cho bạn từ ScrollLayout

final DatePicker dp = (DatePicker) findViewById(R.id.datePicker); 
     final TimePicker tp = (TimePicker) findViewById(R.id.timePicker); 
     btnSet.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       String strDateTime = dp.getYear() + "-" + (dp.getMonth() + 1) + "-" + dp.getDayOfMonth() + " "+ tp.getCurrentHour() + ":" + tp.getCurrentMinute(); 

       Toast.makeText(TimeDate.this, "User selected " + strDateTime + "Time", Toast.LENGTH_LONG).show(); //Generate a toast only if you want 
       finish(); // If you want to continue on that TimeDateActivity 
       // If you want to go to new activity that code you can also write here 
      }}); 
0

Vấn đề của bạn có vẻ là trong dòng này:

ScrollView DTPicker = (ScrollView) View.inflate(TimeDate.this,R.layout.time_date, null); 

Bạn đang lạm phát một XML chứa LinearLayout là nút gốc, tuy nhiên bạn đang type-casting nó để ScrollView gây ra ClassCastException.

2
ScrollView DTPicker = (ScrollView) View.inflate(TimeDate.this,R.layout.time_date, null); 
Button btnSet = (Button) DTPicker.findViewById(R.id.btnSetDateTime); 
final DatePicker dp = (DatePicker) DTPicker.findViewById(R.id.datePicker); 
final TimePicker tp = (TimePicker) DTPicker.findViewById(R.id.timePicker); 

thay thế dòng trên với dưới

ScrollView DTPicker = (ScrollView)findViewById(R.id.scrollView1); 
Button btnSet = (Button)findViewById(R.id.btnSetDateTime); 
final DatePicker dp = (DatePicker)findViewById(R.id.datePicker); 
final TimePicker tp = (TimePicker)findViewById(R.id.timePicker); 
+0

đời xin lỗi nhưng vẫn không làm việc. đây là ngoại lệ con mèo đăng nhập W/dalvikvm (1007): threadid = 1: thread thoát với ngoại lệ không bắt buộc (nhóm = 0x4001d800) E/AndroidRuntime (1007): LOẠI TRỪ KHÓA: chính E/AndroidRuntime (1007): java.lang .RuntimeException: Không thể bắt đầu hoạt động ComponentInfo {com.ogtpl.android/com.ogtpl.android.TimeDate}: java.lang.IllegalStateException: Trẻ được chỉ định đã có cha/mẹ. Bạn phải gọi removeView() trên phụ huynh của đứa trẻ trước. –

+0

hey hãy cố gắng giúp đỡ người đàn ông tôi sẽ surly accpet và upvote câu trả lời của bạn. –

+0

chúng tôi không ở đây để có upvote hoặc chấp nhận nhưng chúng tôi đang ở đây để giúp đỡ khác ... –

0

thử sau mã ....

time_date.xml

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

    <LinearLayout android:id="@+id/llheader" android:layout_width="fill_parent" android:layout_height="20dp" 
     android:layout_weight="1"> 
     <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" 
      android:layout_height="fill_parent" android:layout_gravity="center"> <!--header --> 
      <TextView android:id="@+id/txt_header" android:layout_width="wrap_content" android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" android:layout_height="wrap_content" /> 
     </RelativeLayout> 
    </LinearLayout> 

    <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 
     <LinearLayout android:layout_width="wrap_content" android:id="@+id/linearLayout1" android:orientation="vertical" 
      android:layout_height="wrap_content"> 

      <DatePicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/datePicker" /> 
      <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

      <TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content" android:layout_width="match_parent" 
       android:gravity="center"> 

       <Button android:id="@+id/btnSetDateTime" android:layout_height="wrap_content" android:text="Set" 
        android:layout_width="wrap_content" /> 
       <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Next" 
        android:id="@+id/btnNext1" /> 
      </TableRow> 

     </LinearLayout> 
    </ScrollView> 

    <LinearLayout android:id="@+id/lldata" android:layout_weight="8" android:layout_width="fill_parent" 
     android:layout_height="0dp" android:background="#FFFFFF"> 
    </LinearLayout> 

    <LinearLayout android:id="@+id/llfooter" android:layout_width="fill_parent" android:orientation="horizontal" 
     android:layout_height="20dp" android:visibility="visible" android:layout_margin="0dp"> 
    </LinearLayout> 

</LinearLayout> 

DateTimeActivity.java

public class DateTimeActivity extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.time_date); 
     setTitle("Reminder"); 

     Button btnNext = (Button) findViewById(R.id.btnNext1);   
     btnNext.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent myintent = new Intent(DateTimeActivity.this, Next.class); 
       startActivity(myintent); 
       finish(); 
      } 
     }); 

     Button btnSet = (Button) findViewById(R.id.btnSetDateTime); 
     final DatePicker dp = (DatePicker) findViewById(R.id.datePicker); 
     final TimePicker tp = (TimePicker) findViewById(R.id.timePicker); 

     btnSet.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String strDateTime = dp.getYear() + "-" + (dp.getMonth() + 1) + "-" + dp.getDayOfMonth() + " "+ tp.getCurrentHour() + ":" + tp.getCurrentMinute(); 

       Toast.makeText(DateTimeActivity.this, "User has selected " + strDateTime, Toast.LENGTH_LONG).show(); 

       finish();// move to previous activity 
      }}); 

    }  

} 
+0

Nhờ người đàn ông để được giúp đỡ, nhưng nó đang làm việc như trong một cửa sổ bật lên. tôi muốn điều này sẽ giống như màn hình chính. Nếu bạn muốn tôi có thể cung cấp cho u một ảnh chụp màn hình những gì tôi muốn –

+0

@Vickie Thanh toán câu trả lời được cập nhật. – dira

Các vấn đề liên quan