android上未选择微调器时的java.lang.NullPointerException
当用户未在我的应用程序上选择微调器时,我使用此代码显示提示:android上未选择微调器时的java.lang.NullPointerException,java,android,spinner,android-spinner,Java,Android,Spinner,Android Spinner,当用户未在我的应用程序上选择微调器时,我使用此代码显示提示: protected View getView(int position, View convertView, ViewGroup parent) throws IllegalAccessException { if( position<0 ) { final TextView v= (TextView) ((LayoutInflater)getContext()
protected View getView(int position, View convertView, ViewGroup parent)
throws IllegalAccessException {
if( position<0 ) {
final TextView v= (TextView) ((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).
inflate(android.R.layout.simple_spinner_item, parent, false);
v.setText("--select one--");
return v;
}
return obj.getView(position,convertView,parent);
}
我的应用程序上有55个微调器,当我单击“保存”按钮而我没有选择微调器时,它总是返回空指针。我的问题是,当用户不单击我的微调器时,如何获得空值 可能hubungan_pp.getSelectedItem为空,因为未选择微调器。因此,当您对其调用.toString时,它返回NullPointerException是合乎逻辑的。您可以输入类似的“选择一个”或默认值。或者您可以按如下方式处理空值检查:
String selected_text = "none"
if (hubungan_pp.getSelectedItem() != null)
{
selected_text = hubungan_pp.getSelectedItem().toString();
}
希望这有帮助。是的。。。那么,当空值未被选中时,我如何处理它呢?@AoyamaNanami看到了神秘魔法的答案:
String selected_text = "none"
if (hubungan_pp.getSelectedItem() != null)
{
selected_text = hubungan_pp.getSelectedItem().toString();
}