设置微调器视图文本时,获取空指针异常[重复]

问题描述 投票:-1回答:1

下面是微调器中onItemSelected调用的代码。有时,即使关闭应用程序,我也会得到空指针异常。错误的图片附在这篇文章中。

        /**
         * 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

android view nullpointerexception spinner
1个回答
0
投票

如果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); }
© www.soinside.com 2019 - 2024. All rights reserved.