尝试在android中创建一个微调器:
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);
}
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.
也不起作用。
有人可以帮我解决这个问题吗?
答案 0 :(得分:3)
你需要在一些方法中移动整个代码,你不能在类的方法之外执行代码。
最好将整个代码移到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 :(得分:1)
你不能在班上的某个地方做到这一点。我建议将几乎所有内容放入onCreate()方法
答案 2 :(得分:0)
您应该在Oncreate()中设置适配器。并且还使用setContentView(R.layout.yourlayout)。