2012-11-27 51 views
0

我不斷收到空指針錯誤,但我找不出原因。有什麼非常明顯的我失蹤了嗎?安卓文本在TextView上設置錯誤

final Dialog d = new Dialog(Start.this); 
     // dialog.requestWindowFeature((int) Window.FEATURE_NO_TITLE); 
     d.requestWindowFeature((int) android.view.Window.FEATURE_NO_TITLE); 

     d.setContentView(R.layout.popuptwo); 
     WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
     lp.copyFrom(d.getWindow().getAttributes()); 
     lp.width = WindowManager.LayoutParams.MATCH_PARENT; 
     lp.height = WindowManager.LayoutParams.MATCH_PARENT; 
     d.show(); 

     d.getWindow().setAttributes(lp); 
     TextView totaltxt = (TextView) findViewById(R.id.totaltxt); 
      totaltxt.setText("Test text"); 

如果我刪除totaltxt.setText("Test text");則程序不會崩潰。想法?

+1

後堆棧跟蹤...... – njzk2

+1

(但我的猜測是,你需要調用d.findViewById) – njzk2

+0

確實有totaltxt的TextView在popuptwo佈局? – crbin1

回答

3

文本視圖屬於你應該用對話的看法的dialog..so ..

TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt); 
+0

Upvoted但它應該是d.findViewById – Rawkode

+0

只是編輯它爲「d」 – Carnal

+0

+1是的,非常常見的錯誤:) –

0

只是做.. TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt); totaltxt.setText("Test text");

0

因爲它無法獲取對話框,這樣,

視圖

更換由this--這個line--

TextView totaltxt = (TextView) findViewById(R.id.totaltxt); 
TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt); 

這裏d是你的對話框對象。