簡體   English   中英

如何通過單擊Android中的另一個動態按鈕來禁用動態按鈕

[英]How to disable a Dynamic button by clicking another Dynamic button in android

我在循環中在android動態創建4個按鈕。 在其他情況下,按鈕的數量可以從四個增加。 現在,我試圖在單擊一個按鈕時禁用所有其他按鈕。 我正在嘗試通過按鈕對象數組來做到這一點。

Button btn[] = new Button[4];

for(int i=0;i<4;i++)
{
  TableLayout layout = (TableLayout)findViewById(R.id.tableForEvents);
  btn[i]=new Button(this);
  btn[i].setID(i);
  btn[i].setText("Button"+i);
  layout.addView(btn[i]);
  btn[i].setText(i);  
 }

這段代碼給了我一個空指針異常,並導致應用程序崩潰。 因此,我無法執行啟用或禁用的下一個操作。 請告訴我解決方案,或者是否有其他方法可以解決此問題

確保活動的當前布局中存在R.id.tableForEvents。 您應該將布局設置為null

還將以下代碼移到for循環外(無需實例化多次。)

TableLayout layout = (TableLayout)findViewById(R.id.tableForEvents);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM