我有一个带有选项卡的活动“ ActivityBeamRec”,其中有两个片段,在第二个片段中,当我尝试添加微调器时发生错误,这是片段代码
public class FragBeamRec extends Fragment {
public static FragBeamRec newInstance() {
FragBeamRec fragment = new FragBeamRec();
return fragment;
}
public FragBeamRec() {
}
static EditText tasss = null;
static EditText tass1 = null;
static Spinner spinAs = null;
static Spinner spinAs1 = null;
static ArrayAdapter<String> adapter;
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_frag_beam_rec, container, false);
ArrayList<String> list = new ArrayList<>();
list.add(0,"");
list.add(1,"");
list.add(2,"");
spinAs = (Spinner)v.findViewById(R.id.spinner);
spinAs1 = (Spinner)v.findViewById(R.id.spinner2);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
spinAs.setAdapter(adapter);
return v;
}
}
我尝试使用由
ActivityBeamRec.this
插入的this
,但结束时出现错误“ ActivityBeamRec不是封闭类”。 最佳答案
改变这个
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
至
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, list);
参数必须是上下文。使用
getActivity()
。 http://developer.android.com/reference/android/app/Fragment.html#getActivity%28%29到评论
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});