2012-09-05 48 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)。