作为一个初学者项目(我是编程新手),我想制作一个简单的应用程序,它会显示四个 TextView:当前时间(小时和分钟)、日期、月份日期和年份。它看起来相对简单,我研究了一下,下面的代码就是我想出的。谁能解释为什么我的应用程序强制关闭?我在可用权限列表中没有看到任何关于读取系统时间的内容。提前致谢。(注意:这是我使用的唯一类和布局。)
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" />