2011-11-11 8 views
0

現在、私は日付を取得して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を取得するには、デバッグにそれを実行しようとしたが、それはポートに「添付」するためにあなたがビューV(「公共ボイドsetDateLabelを呼び出している場合は、デバッガがちょうど8634.

答えて

1

を停止して待ちます) '

+0

XMLコードの 'android:onClick =" setDateLabel "で。 – Cistoran

+0

'findViewById'への呼び出しがnullを返すようです。見つかった[this](http://stackoverflow.com/questions/2361509/android-textview-settext-show-nullpointer-exception)、それは役に立ちますか? – Theblacknight

関連する問題