作爲一個初學者項目(我是編程新手),我想創建一個簡單的應用程序,它將顯示四個TextViews:當前時間(小時和分鐘),日期,月份日期和年份。這看起來相對簡單,我研究了下面的代碼就是我想出來的。任何人都可以解釋爲什麼我的應用程序部隊關閉?我在可用權限列表中沒有看到任何有關讀取系統時間的信息。提前致謝。 (注:這是我使用的唯一類和佈局。)檢索系統日期和時間?
MainActivity.java:
package press.linx.calendar;
import android.os.Bundle;
import android.app.Activity;
import android.text.format.Time;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView day = (TextView)findViewById(R.id.day);
TextView month = (TextView)findViewById(R.id.month);
TextView year = (TextView)findViewById(R.id.year);
TextView time = (TextView)findViewById(R.id.Time);
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
day.setText(today.monthDay); // Day of the month (0-31)
month.setText(today.month); // Month (0-11)
year.setText(today.year); // Year
time.setText(today.format("%k:%M:%S")); // Current time
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="89dp"
android:layout_marginTop="32dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/day"
android:layout_marginRight="74dp"
android:layout_marginTop="96dp"
android:text="TextView" />
<TextView
android:id="@+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/day"
android:layout_below="@+id/Time"
android:layout_marginLeft="38dp"
android:layout_marginTop="71dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/month"
android:layout_below="@+id/month"
android:layout_marginTop="64dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
後的logcat ..... – Shiv 2013-04-24 06:57:08
請出示您的logcat錯誤日誌。 – Ercan 2013-04-24 06:57:16
使用日期d = new Date();並獲得小時爲d.getHours(); – Senthil 2013-04-24 07:04:57