[問題] view 與 class 的問題

看板AndroidDev作者 (火焰)時間11年前 (2014/05/22 12:48), 編輯推噓3(302)
留言5則, 2人參與, 最新討論串1/3 (看更多)
大家好 我將一個class (叫做Outter) extends 一個 TextView 然後在 MainActivity的 layout xml 中寫下了 <View class="com.nextzero.testview.Outter" android:id="@+id/outter_layout" android:layout_height="wrap_content" android:layout_width="wrap_content"/> 然後Outter class 有覆寫 TextView原本的constructor (三個都有) public Outter(Context context, AttributeSet attrs, int defStyle) ...略 可是我在 MainActivity中,卻無法將我的View給取出 Outter view = (Outter)findViewById( R.id.outter_layout); 這一行出現 ClassCastException ,可是我完全看不出來哪裡有問題... 可以請問為什麼嗎? 謝謝 附上完整程式碼link: https://dl.dropboxusercontent.com/u/110205128/TestView.zip -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 116.89.142.48 ※ 文章網址: http://www.ptt.cc/bbs/AndroidDev/M.1400734085.A.AC4.html

05/22 13:29, , 1F
<view 改成<com.nextzero.textview.outter 後面class刪
05/22 13:29, 1F

05/22 13:29, , 2F
05/22 13:29, 2F

05/22 13:31, , 3F
好像是因為view 是基礎類,不能把view 強制轉型
05/22 13:31, 3F

05/22 19:49, , 4F
謝謝大大,成功惹。可是我看別人這樣寫卻ok耶QQ
05/22 19:49, 4F

05/23 11:41, , 5F
是哦,我也學沒很久,可以給我你看到的範例我看看嗎?
05/23 11:41, 5F
文章代碼(AID): #1JVO65h4 (AndroidDev)
文章代碼(AID): #1JVO65h4 (AndroidDev)