我的活動中有一半屏幕自定義視圖和一個TextView。在自定義視圖中更新TextView
<com.sted.test.mainView
android:id="@+id/mainView" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView android:id="@+id/tvScore" android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" />
在點擊自定義視圖,我怎麼可以更新我的活動TextView的?
目前我在我的自定義視圖的onTouchEvent()
中有這段代碼,但它在setText()
部分中遇到了NullPointerException。我應該永遠不更新我的自定義視圖中的TextView嗎?
TextView tvScore = (TextView) findViewById(R.id.tvScore);
tvScore.setText("Updated!");
檢查你的setContentView在哪裏給出了正確的佈局,還有在給出setText之前,你必須聲明該textview,如果你做了所有這些事情,那麼在這部分代碼錯誤的錯誤是在這個代碼之外 –