看板 AndroidDev 關於我們 聯絡資訊
請問一下 我的寫法是在 .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
lovelycateye:你要不要把程式碼放上來?我猜是context不同造成Null 06/02 19:28
lovelycateye:看你來你有某些沒說的原因讓你不能宣告在成員變數 06/02 19:29
我連接在下面放 findViewById() 都會出錯...QQ
shietsd:在 oncreate 外面宣告 mText1 => public TextView mText1 06/02 21:01
有很多耶 宣告在外面會宣告不完 ※ 編輯: ashlin1112 來自: 220.141.80.88 (06/04 13:31)
lovelycateye:你要嘛把你程式碼放上來,要嘛把你需求講一下。 06/04 21:47
lovelycateye:因為從你的內容和回文就可以看的出內情不單純 06/04 21:48
就是我現在有一個listView 裡面有很多選項 在擊點選項的時候想對listView裡面的 TextView做修改 謝謝囉 ※ 編輯: ashlin1112 來自: 114.39.169.165 (06/05 00:34)
lovelycateye:那你應該在adapter的getView就做setOnClickListener 06/05 11:28
lovelycateye:https://gist.github.com/5505c4539d8deb4368c6 06/05 12:32