2013-04-24 97 views
0

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

+1

后的logcat ..... – Shiv 2013-04-24 06:57:08

+0

请出示您的logcat错误日志。 – Ercan 2013-04-24 06:57:16

+0

使用日期d = new Date();并获得小时为d.getHours(); – Senthil 2013-04-24 07:04:57

回答

1

找到我得到你的错误。

today.monthDay给出了一个整数值,当您在TextView中设置此值时,它将搜索具有该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 
+0

这可以完美地修复它。如果我希望它在特定时间间隔内轮询时间,它会更新(现在它只显示应用程序打开的时间),我应该如何处理这个问题?提前致谢。 – ThatGuyThere 2013-04-25 00:33:13

+0

你可以通过使用'CountDownTimer'和'onFinish()'方法来更新你的'Views'。下面是如何使用'CountDownTimer'的链接http://developer.android.com/reference/android/os/CountDownTimer.html – 2013-04-29 06:48:12

-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 
0

您可以使用Java util的和的SimpleDateFormat和日期相同的目的:

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)

相关问题