2012-07-20 62 views
0

我在Activity中有兩個調整器,第二個Spinner的選擇集基於用戶爲第一個Spinner選擇的內容。我在Activity中使用了一個私有類變量,該變量在頂部Spinner的OnItemSelectedListener中設置,然後在底部Spinner的OnItemSelectedListener中引用以獲取正確的選擇集。OnCreate後的OnItemSelectedListener調用次序

這幾乎總是有效,但有時(主要是當應用程序運行,不退出,然後由用戶再次點擊很長時間後再次啓動)我得到一個空指針異常在第二個微調的OnItemSelectedListener由於這個局部變量沒有被設置。這表明在OnCreate之後,第二個Spinner的OnItemSelectedListener在第一個Spinner之前被調用。

是否有任何方法可以強制偵聽器中的某個命令被觸發或者是否有更好的設計方法來處理第二個Spinner對第一個Spinner的依賴?

示例代碼: package com.crashtestdummylimited.navydecoder;

public class Test extends Activity {  

    // Variable that at times is still null 
    private ReferenceData  referenceData;  

    private void setupSpinnerFromArray (int spinnerId, String stringArray[], OnItemSelectedListener listener) {  
    Spinner spinner = (Spinner) findViewById(spinnerId);  

    ArrayAdapter <CharSequence> adapter = new ArrayAdapter <CharSequence>( 
     this, android.R.layout.simple_spinner_item, stringArray);  

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  

    spinner.setAdapter(adapter);  

    spinner.setOnItemSelectedListener(listener);  
    }  

    @Override  
    public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  

    setContentView(R.layout.main_screen);  

    // Setup Top (main) Spinner  
    Spinner spinner1 = (Spinner) findViewById(R.id.mainDecodeSpinner);  

    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource( 
     this, R.array.level0_list_array, android.R.layout.simple_spinner_item);  

    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
    spinner1.setAdapter(adapter1);  

    spinner1.setOnItemSelectedListener(new MainDecoderItemSelectedListener());  


    // Setup Bottom (dependent) Spinner  
    setupSpinnerFromArray(R.id.secondaryDecodeSpinner, R.array.level1_list_array, new SecondaryDecoderItemSelectedListener());  
    }  



    public class MainDecoderItemSelectedListener implements OnItemSelectedListener {  

    public void onItemSelected(AdapterView<?> parent,  
           View view, int pos, long id) {  



     String selectedString = parent.getItemAtPosition(pos).toString();  

     if (selectedString.equals("AAA")){  
     // Problem variable is set 
     referenceData = new RatingCodes();  
     setupSpinnerFromArray(R.id.secondaryDecodeSpinner, referenceData.getKeys(), new SecondaryDecoderItemSelectedListener());  
     }   
     else if (selectedString.equals("BBB")){  
     // Problem variable is set 
     referenceData = new IMSCodes();  
     setupSpinnerFromArray(R.id.secondaryDecodeSpinner, referenceData.getKeys(), new SecondaryDecoderItemSelectedListener());  
     }  
     // TODO: Improve what occurs if no match which should not occur  
    }  

    public void onNothingSelected(AdapterView<?> parent) {  
     // Do nothing.  
    }  
    }  


    public class SecondaryDecoderItemSelectedListener implements OnItemSelectedListener {  

    public void onItemSelected(AdapterView<?> parent,  
           View view, int pos, long id) {  

     String key = parent.getItemAtPosition(pos).toString();  

     // **** referenceData being null at this point has caused crashed ****  
     String value = referenceData.getValue(key);  

     // ... Update text on activity screen ...  
    }  

    public void onNothingSelected(AdapterView<?> parent) {  
     // Do nothing.  
    }  
    }  

}  
+0

會更清,如果您發佈的代碼... – 2012-07-20 03:41:41

+0

經進一步審查,我猜這個問題是由於兩種: 1)的Android可能會被解僱的主要微調監聽器(MainDecoderItemSelectedListener)當Spinner沒有選擇一個值時(來自R.array.level0_list_array)。這肯定會創建空指針異常,但我會假設Spinner始終必須從R.array.level0_list_array獲得有效值。我可以通過使用當前TODO所在的某些默認值來克服這一點。 2)不知何故,在MainDecoderItemSelectedListener觸發之前,SecondaryDecoderItemSelectedListener正在觸發。 – Mike 2012-07-21 12:14:43

回答

0
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) 
{ 

if(pos == 0) 
{ //when it loads on onCreate() then pos is always 0 
     //donothing    
    } 
else 
{ //If user manually select item 

      //do what you need to do on manual user selection 

} 
} 
相關問題