设置微调器视图文本时出现空指针异常
Getting null pointer exception when setting spinner view text
下面是在微调器中调用 onItemSelected 的代码。有时即使应用程序关闭,我也会收到空指针异常。 bug 的图片附在这个 post.
/**
* Store the selected product
* @param parent
* @param view
* @param position
* @param id
*/
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
QuickListItem selectedSpinnerProduct = productAdapter.getItem(position);
if (selectedSpinnerProduct != null) {
mgrApp.storeSharedPrefLong(ConstantsQuickList.SHARED_PREF_PRODUCT_ID,
selectedSpinnerProduct.getId());
}
SpannableStringBuilder builder = getPendingCallCount(products,
selectedSpinnerProduct);
((TextView) view).setText(builder, TextView.BufferType.SPANNABLE);
//Hide the performance tracker as product will change
ViewProspects.crdvPerformanceTracker.setVisibility(View.GONE);
populateSalesTracker(typeOfOperation,ConstantsProspects.INVALID_STRING);
}
Application cashed bug image
如果 selectedSpinnerProduct 为空,您将遇到问题。因此,请在所有地方检查 selectedSpinnerProduct 是否为 null。
if (selectedSpinnerProduct != null) {
mgrApp.storeSharedPrefLong(ConstantsQuickList.SHARED_PREF_PRODUCT_ID,
selectedSpinnerProduct.getId());
SpannableStringBuilder builder = getPendingCallCount(products,
selectedSpinnerProduct);
((TextView) view).setText(builder, TextView.BufferType.SPANNABLE);
}
下面是在微调器中调用 onItemSelected 的代码。有时即使应用程序关闭,我也会收到空指针异常。 bug 的图片附在这个 post.
/**
* Store the selected product
* @param parent
* @param view
* @param position
* @param id
*/
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
QuickListItem selectedSpinnerProduct = productAdapter.getItem(position);
if (selectedSpinnerProduct != null) {
mgrApp.storeSharedPrefLong(ConstantsQuickList.SHARED_PREF_PRODUCT_ID,
selectedSpinnerProduct.getId());
}
SpannableStringBuilder builder = getPendingCallCount(products,
selectedSpinnerProduct);
((TextView) view).setText(builder, TextView.BufferType.SPANNABLE);
//Hide the performance tracker as product will change
ViewProspects.crdvPerformanceTracker.setVisibility(View.GONE);
populateSalesTracker(typeOfOperation,ConstantsProspects.INVALID_STRING);
}
Application cashed bug image
如果 selectedSpinnerProduct 为空,您将遇到问题。因此,请在所有地方检查 selectedSpinnerProduct 是否为 null。
if (selectedSpinnerProduct != null) {
mgrApp.storeSharedPrefLong(ConstantsQuickList.SHARED_PREF_PRODUCT_ID,
selectedSpinnerProduct.getId());
SpannableStringBuilder builder = getPendingCallCount(products,
selectedSpinnerProduct);
((TextView) view).setText(builder, TextView.BufferType.SPANNABLE);
}