【问题标题】:Getting And Displaying Date Info In Android在 Android 中获取和显示日期信息
【发布时间】:2011-11-11 19:59:25
【问题描述】:

目前我正在尝试获取日期(特别是月份和年份)并使用 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>

我尝试在 Debug 中运行它以获取 LogCat,但调试器只是停止并等待它“附加”到端口 8634。

【问题讨论】:

    标签: java android calendar logcat forceclose


    【解决方案1】:

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

    【讨论】:

    • 您对“findViewById”的调用似乎返回了 null。找到this,有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    相关资源
    最近更新 更多