在Android中获取和显示日期信息
问题描述:
当前我正在尝试获取日期(特定月份和年份)并使用TextView显示它,但是我的程序正在强制关闭。在Android中获取和显示日期信息
这里是我“得到”月份和年份。
Calendar cCalendar = Calendar.getInstance();
int currentYear = cCalendar.get(Calendar.YEAR);
int currentMonth = cCalendar.get(Calendar.MONTH);
这里是我设置TextView文本的位置。
public void setDateLabel(View v)
{
TextView month = (TextView)findViewById(R.id.monthLabel);
TextView year = (TextView)findViewById(R.id.yearLabel);
year.setText(currentYear);
month.setText(currentMonth);
}
这是带有TextView和Button的XML。
<TextView android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/monthLabel"></TextView>
<TextView android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/yearLabel"></TextView>
<Button android:text="Button" android:onClick="setDateLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/setDate"></Button>
我尝试在调试运行它得到一个logcat的但是调试器只是停止,并等待其到端口8634.
答
“附加”当你打“公共无效setDateLabel(视图V ) '
在XML代码中的android:onClick =“setDateLabel”中。 – Cistoran
看来你对'findViewById'的调用返回null。发现[这](http://stackoverflow.com/questions/2361509/android-textview-settext-show-nullpointer-exception),它有帮助吗? – Theblacknight