Android &引用;setAdapter";片段中的微调器不起作用
我正在尝试实现微调器,并且在生产线上Android &引用;setAdapter";片段中的微调器不起作用,android,android-layout,android-fragments,fragment,Android,Android Layout,Android Fragments,Fragment,我正在尝试实现微调器,并且在生产线上 spinner1.setAdapter(adapter1); 我得到以下错误: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)” 这是我的代码: public class Temp extends Fragment implements AdapterView.OnItem
spinner1.setAdapter(adapter1);
我得到以下错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)”
这是我的代码:
public class Temp extends Fragment implements AdapterView.OnItemSelectedListener{
private static final String[] paths1 = {"sam1", "sam2", "sam3", "sam4"},
paths2 = {"1", "2", "3", "4"};
private String name,seq;
private Spinner spinner1,spinner2;
private TextView tv1;
private ArrayAdapter<String> adapter1,adapter2;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View attendFragView = inflater.inflate(R.layout.fragment_attendance, container, false);
//Spinner 1 code
spinner1=(Spinner)attendFragView.findViewById(R.id.regPageFacSpinner1);
adapter1=new ArrayAdapter<String>(attendFragView.getContext(),R.layout.spinner_lay_notched,
paths2);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(this);
//Spinner 2 Code
spinner2=(Spinner)attendFragView.findViewById(R.id.regPageFacSpinner2);
adapter2=new ArrayAdapter<String>(attendFragView.getContext(),R.layout.spinner_lay_notched,
paths1);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(this);
final EditText et1 = (EditText) attendFragView.findViewById(R.id.secOtpFac);
tv1=(TextView)attendFragView.findViewById(R.id.attendPageFac5);
Button genOtp = (Button) attendFragView.findViewById(R.id.otpGenButFac);
genOtp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String dept= et1.getText().toString();
String name=spinner1.getSelectedItem().toString();
String seq=spinner2.getSelectedItem().toString();
JSONObject attendJSONSend = new JSONObject();
try {
attendJSONSend.put("name", name);
attendJSONSend.put("seq", seq);
attendJSONSend.put("dept", dept);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return attendFragView;
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
公共类临时扩展片段实现AdapterView.OnItemSelectedListener{
私有静态最终字符串[]路径1={“sam1”、“sam2”、“sam3”、“sam4”},
路径2={“1”、“2”、“3”、“4”};
私有字符串名称,seq;
专用喷丝头1、喷丝头2;
私有文本视图tv1;
专用阵列适配器1、适配器2;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater充气机,@null可查看组容器,
@可为空的捆绑包savedInstanceState){
视图AttendantFragView=充气机。充气(R.layout.fragment\u Attendant,容器,false);
//微调器1代码
spinner1=(微调器)attendFragView.findViewById(R.id.regPageFacSpinner1);
adapter1=新阵列适配器(attendFragView.getContext(),R.layout.spinner\u lay\u凹口,
路径2);
adapter1.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
喷丝头1.设置适配器(适配器1);
喷丝头1.SetonimSelectedListener(此);
//微调器2代码
spinner2=(微调器)attendFragView.findViewById(R.id.regPageFacSpinner2);
adapter2=新阵列适配器(attendFragView.getContext(),R.layout.spinner\u lay\u凹口,
路径1);
adapter2.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
喷丝头2.设置适配器(适配器2);
喷丝头2.SetonimSelectedListener(此);
最终EditText et1=(EditText)attendFragView.findViewById(R.id.secOtpFac);
tv1=(TextView)attendFragView.findViewById(R.id.attendPageFac5);
按钮genOtp=(按钮)attendFragView.findViewById(R.id.otpGenButFac);
genOtp.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
字符串dept=et1.getText().toString();
字符串名称=spinner1.getSelectedItem().toString();
String seq=spinner2.getSelectedItem().toString();
JSONObject attendJSONSend=新JSONObject();
试一试{
attendJSONSend.put(“name”,name);
attendJSONSend.put(“seq”,seq);
attendJSONSend.put(“dept”,dept);
}捕获(JSONException e){
e、 printStackTrace();
}
}
});
返回AttendantFragView;
}
@凌驾
已选择公共视图(AdapterView AdapterView、View视图、int i、long l){
}
@凌驾
未选择公共无效(AdapterView AdapterView){
}
}
如例外情况所述,spinner1
为空,因为编译器试图在空对象上使用setAdapter
建议以解决此问题。(因为我经常面对它)
- 检查布局,确保代码和XML布局文件中的视图ID正确
- Android Studio将保存您创建的视图ID,尽管您以前可能已删除它们。尝试清理/重建
- 更具体地说,问题出在这个id上:
R.id.regPageFacSpinner1
祝你好运。你能添加你的xml文件吗?尝试更改
新的ArrayAdapter(attendFragView.getContext(),R.layout.spinner\u lay\u notched,paths2)代码>到新阵列适配器(getActivity(),R.layout.spinner\u lay\u notched,paths2)
你确定带有regPageFacSpinner1
id的微调器在fragment\u
布局中吗?我挖掘最后一行。Android开发有时确实需要运气。@Mauker感谢您的编辑。不客气!