[問題] setId() 和 findViewById() 的使用

看板AndroidDev作者 (破紀錄吧 費拔)時間13年前 (2012/06/02 18:27), 編輯推噓4(403)
留言7則, 2人參與, 最新討論串1/1
請問一下 我的寫法是在 .java 檔裡面 用 addView 一個一個元件放進去 而非使用一般 /res/layout/XXX.xml的寫法 現在我想幫 TextView 加上ID 以便在其他的地方修改textview的內容 寫法是 TextView mText1 = new TextView(mProject.this); mText1.setId(1010101); mText1.setText("123456"); 然後在別的地方寫 TextView mText1 = (TextView)findViewById(1010101); mText1.setText("asdqwe"); 然後就出現 nullpointerexception 的錯誤訊息 然後強制關閉 之後有測試在別的地方寫 TextView mText1 = new TextView(mProject.this); mText1.findViewById(1010101); mText1.setText("asdqwe"); 這樣是不會出錯了 但也沒什麼反應... 請問版上的神人 要怎麼解決這個問題? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.246.208

06/02 19:28, , 1F
你要不要把程式碼放上來?我猜是context不同造成Null
06/02 19:28, 1F

06/02 19:29, , 2F
看你來你有某些沒說的原因讓你不能宣告在成員變數
06/02 19:29, 2F
我連接在下面放 findViewById() 都會出錯...QQ

06/02 21:01, , 3F
在 oncreate 外面宣告 mText1 => public TextView mText1
06/02 21:01, 3F
有很多耶 宣告在外面會宣告不完 ※ 編輯: ashlin1112 來自: 220.141.80.88 (06/04 13:31)

06/04 21:47, , 4F
你要嘛把你程式碼放上來,要嘛把你需求講一下。
06/04 21:47, 4F

06/04 21:48, , 5F
因為從你的內容和回文就可以看的出內情不單純
06/04 21:48, 5F
就是我現在有一個listView 裡面有很多選項 在擊點選項的時候想對listView裡面的 TextView做修改 謝謝囉 ※ 編輯: ashlin1112 來自: 114.39.169.165 (06/05 00:34)

06/05 11:28, , 6F
那你應該在adapter的getView就做setOnClickListener
06/05 11:28, 6F
文章代碼(AID): #1FoUgN9U (AndroidDev)