1

我遇到了問題我相信解決方案相對簡單,但我錯過了一些東西。基本上,我的程序從用戶端流向的是,我有一個「主屏幕」,其中的屏幕按鈕是半透明的,沒有響應,直到從下拉菜單(微調控件)中選擇一個項目。 在代碼方面,我已經在XML文件中定義了佈局,然後只編寫了所有關聯的Java。所以在onCreate()方法我已經初始化按鈕,如下所示:使用回調和處理程序在Android中更改UI元素

public void initButtons() { 
     Button buttonOne = (Button) findViewById(R.id.button_one); 
     Button buttonTwo = (Button) findViewById(R.id.button_two); 
     Button buttonThree = (Button) findViewById(R.id.button_three); 
     Button buttonFour = (Button) findViewById(R.id.button_four); 

     buttonOne.setClickable(false); 
     buttonTwo.setClickable(false); 
     buttonThree.setClickable(false); 
     buttonFour.setClickable(false); 

     buttonOne.setAlpha(0.5f); 
     ButtonTwo.setAlpha(0.5f); 
     buttonThree.setAlpha(0.5f); 
     buttonFour.setAlpha(0.5f); 
    } 

這工作正常,但我想設置一些onClickListeners後的項目已經從我的微調選擇。因此,我的活動類實現onItemClickListener,我有我的代碼的回調:

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, 
     long id) { 
    if (position != 0) { 
     selectedTown = parent.getItemAtPosition(position).toString(); 
     CharSequence text = "You've Selected " + selectedItem; 
     int duration = Toast.LENGTH_SHORT; 
     Toast toast = Toast.makeText(SampleUI.this, text, duration); 
     toast.show(); 
     itemSelected = true; 
     handler.sendEmptyMessage(1); 
    } else { 
     itemSelected = false; 
    } 

} 

@Override 
public void onNothingSelected(AdapterView<?> arg0) { 

} 

布爾itemSelected是活動類中全球範圍的,麪包是爲了調試的目的,而不是必不可少的。但是我遇到的問題是,當我在微調器上的一個項目(位置0下方)被選中後嘗試更改按鈕時。我已經定義,像這樣一個消息處理程序:

Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      if ((itemSelected == true) && (buttonsActivated == false)) { 
       int duration = Toast.LENGTH_SHORT; 
       Toast toast = Toast.makeText(SampleUI.this,"Activating Buttons", duration); 
       toast.show(); 
       activateButtons(); 
      } 
      super.handleMessage(msg); 
     } 
    }; 

該處理器正常工作,直到我叫activateButtons()方法,該方法是這樣的:

public void activateButtons() { 
     buttonOne.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //Code to respond to buttonClick 
      } 
     }); 
} 

每當我試着與UI元素進行交互在回調函數或處理函數中,我得到一個Java NULL指針異常。我很肯定這是我錯過的相當簡單和基本的東西,但我似乎無法發現問題。如果任何人有任何想法,我很想聽聽你從:) 威爾SJ

回答

1

申報在類級別而不是作爲內部方法所有UI元素:

//********** DELCARE ALL UI ELEMETS HERE ****** 

    Button buttonOne,buttonTwo,buttonThree,buttonFour; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      initButtons(); 
     } 

    public void initButtons() { 
      buttonOne = (Button) findViewById(R.id.button_one); 
      buttonTwo = (Button) findViewById(R.id.button_two); 
      buttonThree = (Button) findViewById(R.id.button_three); 
      buttonFour = (Button) findViewById(R.id.button_four); 
      // your code here... 
     } 
+0

如果在所有編譯的問題的代碼,這可能意味着所有的按鈕被宣佈兩次:一次是在一流水平,一旦該方法。所以你的答案進入正確的方向。但最有可能的是,威爾·斯科特 - 傑克遜只需要按照你提出的方式修復'initButtons()'方法。 – Codo

+1

感謝你們解決了我的問題:D就像你說的,只是在類級別聲明它,而不是方法級別,它現在都在工作!感謝幫助 –

1

使用以下模式

public void initButtons(){  
    Button buttonOne = (Button)findViewById(R.id.button_one); 
    Button buttonTwo = (Button)findViewById(R.id.button_two); 
    Button buttonThree = (Button)findViewById(R.id.button_three); 
    Button buttonFour = (Button)findViewById(R.id.button_four); 

    buttonOne.setClickable(false); 
    buttonTwo.setClickable(false); 
    buttonThree.setClickable(false); 
    buttonFour.setClickable(false); 

    buttonOne.setEnabled(false); 
    buttonTwo.setEnabled(false); 
    buttonThree.setEnabled(false); 
    buttonFour.setEnabled(false); 

    buttonOne.setAlpha(0.5f); 
    buttonTwo.setAlpha(0.5f); 
    buttonThree.setAlpha(0.5f); 
    buttonFour.setAlpha(0.5f); 

    buttonOne.setOnClickListener(this); 
    buttonTwo.setOnClickListener(this); 
    buttonThree.setOnClickListener(this); 
    buttonFour.ssetOnClickListener(this); 


} 

,讓您的活動實現onClickListener,並有一個開關的情況下這樣

public void onClick(View v){ 
    switch (v.getId()){ 
    case R.id.buttonOne : 
// to do when clicked 
     break; 
    case R.id.buttonTwo : 

     break; 
    default : 
     break; 
    } 
} 

和inide您的處理程序調用

public void activateButtons() { 
    buttonOne.setClickable(true); 
    buttonOne.setEnabled(true); 
} 
+0

這對我稍後在代碼中需要做的事情非常有幫助謝謝:) –

相關問題