2012-09-05 44 views
1

試圖讓一個微調在安卓 setdropdownviewresource標識預期

package com.example.test; 
import android.app.Activity; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.BaseAdapter; 

public class SpinnerBuilding extends Activity { 
Spinner spinner = (Spinner) findViewById(R.id.building); 

// Create an ArrayAdapter using the string array and a default spinner layout 
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
     R.array.buildings_array, android.R.layout.simple_spinner_item); 

// Specify the layout to use when the list of choices appears 
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

// Apply the adapter to the spinner 
spinner.setAdapter(adapter); 
} 

它拋出一個錯誤說"Syntax error on token "setDropDownViewResource", identifier expected after this token.此外,spinner.setAdapter(adapter);也不起作用。

有人能幫助我嗎?

回答

2

您需要將某個方法中的整個代碼移動到內部,您無法從類的外部方法執行代碼。

最好將整個代碼移到Oncreate中。

public class SpinnerBuilding extends Activity { 
public void onCreate(Bundle b){ 

super.onCreate(b); 
setContentView(R.id.layout); 
Spinner spinner = (Spinner) findViewById(R.id.building); 

// Create an ArrayAdapter using the string array and a default spinner layout 
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
     R.array.buildings_array, android.R.layout.simple_spinner_item); 

// Specify the layout to use when the list of choices appears 
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

// Apply the adapter to the spinner 
spinner.setAdapter(adapter); 
} 
} 
+1

對......我忘了......太棒了......謝謝你!所以注意我的其他代碼,我忘了這... – JHoo

+0

似乎無法得到它的作品...現在沒有語法錯誤。然而,微調並沒有顯示出來。我想知道爲什麼..? – JHoo

+0

哦,我現在明白了。謝謝你! – JHoo

1

你不能在你班上的某個地方這樣做。我建議你把幾乎所有的東西放到onCreate()方法中

0

你應該在Oncreate()中設置適配器。還可以使用setContentView(R.layout.yourlayout)。