私はユーザーに時間を選択するように頼もうとしているアプリを構築しているので、とを使用datePicker
しtimePicker
ましたbutton set
。したがって、彼が来てクリックset button
するdialog box
と、x日付とx時刻を選択したように、または何らかの方法でユーザーが今回選択したようにメッセージが画面に表示されます。だから私はコードを作成しましたが、私のアプリがそのアクティビティに到達すると、常に強制的に停止しました。とxml file
から値をフェッチするJava行にコメントするdatePicker
とtimePicker
、アプリは完全に正常に動作するため、データの表示に関連する問題はありません。それでも私は理解しやすいように両方のファイルのコードを投稿しています。logcat例外も投稿します。インポートやパッケージ、余分なボタンなどの不要なコードをすべて削除して、すばやく読みやすくしました。
これは.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