我有一个标签式活动“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不是封闭的类”。
答案 0 :(得分:3)
更改此
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
到
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, list);
param需要是一个上下文。使用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
}
});
答案 1 :(得分:0)
更改此行
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
到这个
adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, list);
答案 2 :(得分:0)
这将是help you
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, list);