我有3个微调器,第一个微调器是txt_kategori
;
第二个txt_kondisi
;
第三个txt_penanganan
;第二个取决于第一个和第三个取决于第二个输入,这是我的代码
txt_kategori.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_spinner_item, condition[position]);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
txt_kondisi.setAdapter(spinnerArrayAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
txt_kondisi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_spinner_item, handling[position]);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
txt_penanganan.setAdapter(spinnerArrayAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
问题是java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.View.getContext()' on a null object reference
在ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_spinner_item, handling[position]);
答案 0 :(得分:0)
使用ActivityName.this
代替view.getContext()
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(view.getContext(), android.R.layout.simple_spinner_item, condition[position]);
更改为
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(ActivityName.this, android.R.layout.simple_spinner_item, condition[position]);