下面的方法執行您在xml中定義的onClick方法的操作。
android:onclick="fade"
void public fade(View view){
// Code for button onClick action .
}
每當在用戶點擊按鈕這種方法將觸發和代碼的方法內運行。
ImageView obj = (ImageView) findViewById(R.id.demo);
此代碼意味着你在XML文件作爲例如在ImageView的:
<ImageView
android:id = "@+id/demo"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@drawable/ic_launcher" />
你引用或使用該ID是由法findViewById();
你的情況的演示找到ImageView的。 如果在活動xml中沒有任何ImageView,則會得到空指針異常。檢查您的活動xml的合適名稱。