0

作为一个初学者项目(我是编程新手),我想制作一个简单的应用程序,它会显示四个 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" />

4

3 回答 3

1

我明白你的错误。

today.monthDay给出一个整数值,当你在 a 中设置 this 时,TextView它会搜索带有 that 的资源id。你一定会得到一个Resources$NotFoundException.

试试这个:

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
于 2013-04-24T07:15:45.430 回答
0

您可以将 Java util 和 SimpleDateFormat 和 Date 用于相同目的:

long time = System.currentTimeMillis();

// Day of the month (0-31)
SimpleDateFormat sdf = new SimpleDateFormat("d");
String day = sdf.format(new Date(time));
day.setText(day);

// month
sdf = new SimpleDateFormat("M");
String month = sdf.format(new Date(time));
month.setText(month);

// year
sdf = new SimpleDateFormat("y");
String year = sdf.format(new Date(time));
year.setText(year);

// time
sdf = new SimpleDateFormat("K:mm:ss a");
String time = sdf.format(new Date(time));
time.setText(time);

所有时间字符串格式都可以在http://developer.android.com/reference/java/text/SimpleDateFormat.html#SimpleDateFormat(java.lang.String)

于 2013-04-24T07:15:43.807 回答
-1

使用此 java utils 方法检索系统当前日期和时间。

Time dtNow = new Time();
dtNow.setToNow();
String sytem_time_date = dtNow.format("%Y-%m-%d %H:%M:%S"); //use single text view to show
于 2013-04-24T07:04:38.343 回答