8

我正在构建一个应用程序,我试图在其中要求用户选择时间,所以我使用了datePickerandtimePickerbutton set. 所以当他点击set button一个dialog box来说你已经选择了x日期和x时间或者以任何方式出现在屏幕上的消息就像用户选择了这个时间一样。所以我构建了代码,但是当我的应用程序进入该活动时,它总是被强制停止。显示数据相关没有问题,因为当我评论从andxml file获取值的 java 行时,应用程序工作得非常好。我仍然发布两个文件代码以便于理解。还发布了日志猫异常。我已经删除了所有不必要的代码,如导入和包以及额外的按钮等,以使其快速阅读。datePickertimePicker

这是.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"
     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>

这是代码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();

 }       
}

这是一个logcat例外:

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
4

4 回答 4

13

不要做任何额外的事情,你的代码就是完美的。只需在您使用的任何地方删除代码ScrollLayout或其属性即可。Lemme 为您编写更新的代码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
            }});
于 2012-05-02T12:02:26.637 回答
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);

用下面替换上面的行

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);
于 2012-04-27T07:59:02.203 回答
0

您的问题似乎在这一行:

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

您正在膨胀包含LinearLayout作为根节点的 XML,但是您正在将其类型转换为ScrollView导致ClassCastException.

于 2012-04-27T07:57:24.307 回答
0

try following code....

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
            }});

    }       

}
于 2012-05-01T11:40:20.217 回答