java - 在 Android 中获取和显示日期信息

标签 java android calendar logcat forceclose

目前,我正在尝试获取日期(特别是月份和年份)并使用 TextView 显示它,但我的程序正在强制关闭。

这是我“获取”月份和年份的地方。

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 上。

最佳答案

你什么时候调用'public void setDateLabel(View v) '

关于java - 在 Android 中获取和显示日期信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8099254/

相关文章:

android - 如何在 React-native 项目中找到应用程序文件夹路径

java - java中的日历日期为yyyy-MM-dd格式

java - 从用户输入获取星期几

java - 使用 Maven 的 WAR 中的高级资源排除规则

java - 如何在java中使用nextLine获取多个输入

java - SunCertPathBuilderException : unable to find valid certification path to requested target`

java - 了解 getViewTreeObserver

java - 空的 editText 字段在所有字段上显示错误 [Android]

java - 制作一个可以绘制到 Canvas 上的类

javascript - 输入按钮 - Onclick 换色器