【发布时间】:2015-01-18 18:42:42
【问题描述】:
我正在学习 android 编程并开发了一个小型应用程序,其中包括同一屏幕上的 2 个日期选择器(开始日期和结束日期)。
它适用于 android 版本 2.3,但当我在 android 版本 4.4 中设置日期时崩溃。
下面是java代码sn-p。
公共类 WriteExcelActivity 扩展 Activity {
private TextView startDateDisplay;
private TextView endDateDisplay;
private Button startPickDate;
private Button endPickDate;
private Calendar startDate;
private Calendar endDate;
static final int DATE_DIALOG_ID = 0;
private TextView activeDateDisplay;
private Calendar activeDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_writeexcel);
/* capture our View elements for the start date function */
startDateDisplay = (TextView) findViewById(R.id.fromdate);
startPickDate = (Button) findViewById(R.id.imageButton1);
startDate = Calendar.getInstance();
/* add a click listener to the button */
startPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDateDialog(startDateDisplay, startDate);
}
});
/* capture our View elements for the end date function */
endDateDisplay = (TextView) findViewById(R.id.todate);
endPickDate = (Button) findViewById(R.id.imageButton2);
/* get the current date */
endDate = Calendar.getInstance();
/* add a click listener to the button */
endPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDateDialog(endDateDisplay, endDate);
}
});
/* display the current date (this method is below) */
updateDisplay(startDateDisplay, startDate);
updateDisplay(endDateDisplay, endDate);
}
private void updateDisplay(TextView dateDisplay, Calendar date) {
dateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(date.get(Calendar.MONTH) + 1).append("-")
.append(date.get(Calendar.DAY_OF_MONTH)).append("-")
.append(date.get(Calendar.YEAR)).append(" "));
}
public void showDateDialog(TextView dateDisplay, Calendar date) {
activeDateDisplay = dateDisplay;
activeDate = date;
showDialog(DATE_DIALOG_ID);
}
private OnDateSetListener dateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
activeDate.set(Calendar.YEAR, year);
activeDate.set(Calendar.MONTH, monthOfYear);
activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(activeDateDisplay, activeDate);
unregisterDateDisplay();
}
};
private void unregisterDateDisplay() {
activeDateDisplay = null;
activeDate = null;
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
break;
}
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true; }
如果有人能指出错误,不胜感激。谢谢
=============================================
xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toDate"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textbox_fromdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/txt_textbox_fromdate"
android:layout_marginTop="22dp" />
<TextView
android:id="@+id/fromdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textbox_fromdate"
android:ems="10"
/>
<Button
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/fromdate"
android:layout_toRightOf="@+id/fromdate"
android:drawableLeft="@drawable/ic_datepicker"
/>
<TextView
android:id="@+id/textbox_todate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/txt_textbox_todate"
android:layout_below="@+id/fromdate" />
<TextView
android:id="@+id/todate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textbox_todate"
/>
<Button
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/todate"
android:layout_toRightOf="@+id/todate"
android:drawableLeft="@drawable/ic_datepicker"
android:contentDescription="" />
<Button
android:id="@+id/btnexcel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/todate"
android:onClick="onClickWriteExcelData"
android:text="Write excel data" />
</RelativeLayout>
enter code here
【问题讨论】:
-
logcat 中的崩溃是什么?
-
我正在将apk文件复制到手机上进行安装和运行。因此不确定在哪里可以看到 logcat 消息。该设备没有出现在android studio中..所以不得不这样做......
-
如果没有 logcat 输出,您将很难解决这个问题。您是否尝试过通过命令行(adb)获取它?检查这个:developer.android.com/tools/help/logcat.html
标签: android