2011-08-14 126 views
3

我很努力地動態創建微調框。我在頁面上有它,但每次我試圖選擇一個選項,它會炸燬。我的原始代碼位於底部。我固定它通過移動addSpinner()函數內部類之外,改變動態創建微調框

Spinner newSpinner = new Spinner(getApplicationContext()); 

Spinner newSpinner = new Spinner(this); 

它是固定的,但我不知道它最初沒有工作。誰能解釋一下? (道歉,如果這是一個小白的問題 - 我是新來的Java和Android設備)

public class SpotManageActivity extends Activity 
{ 
    private SimpleCursorAdapter mSpots; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.reminder_manage_activity); 

     Button add_new_button = (Button) findViewById(R.id.add_new_spot_button); 
     add_new_button.setOnClickListener(new ButtonOnClickListener()); 

    } 

    public class ButtonOnClickListener implements View.OnClickListener 
    { 
     @Override 
     public void onClick(View v) 
     { 
      addSpinner(); 
     } 

     private void addSpinner() 
     { 
      LinearLayout layoutHolder = 
       (LinearLayout) findViewById(R.id.layout_holder); 

      LinearLayout spinnerHolder = new LinearLayout(getApplicationContext()); 
      spinnerHolder.setOrientation(LinearLayout.HORIZONTAL); 

      spinnerHolder.setLayoutParams(
       new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.FILL_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT, 1f)); 
      Spinner newSpinner = new Spinner(getApplicationContext()); 
      newSpinner.setLayoutParams(
       new Spinner.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT)); 
      newSpinner.setAdapter(mSpots); 

      layoutHolder.addView(spinnerHolder); 
      spinnerHolder.addView(newSpinner); 

      // A non-internal spot was selected 
     } 

    } 

} 
+0

代碼審查是嚴格的改進工作代碼。修復關於代碼工作方式/原因的代碼或問題不屬於此處。如果您希望獲得進一步的幫助,只要您明白,隨時可以將其恢復。 –

回答