当前位置: 代码迷 >> java >> 在RecyclerView的onBindViewHolder方法中将文本设置为EditText时出错
  详细解决方案

在RecyclerView的onBindViewHolder方法中将文本设置为EditText时出错

热度:45   发布时间:2023-07-17 20:33:26.0

我正在尝试将文本设置为我的自定义适配器中的EditText ,我正在获取此堆栈:

java.lang.NullPointerException: Attempt to read from field 'android.support.v7.widget.ViewInfoStore android.support.v7.widget.RecyclerView.mViewInfoStore' on a null object reference
    at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:8194)
    at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:8180)
    at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:8168)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1573)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
    at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    at android.view.View.measure(View.java:17547)
    at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1260)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
    at android.widget.ScrollView.onMeasure(ScrollView.java:337)
    at android.view.View.measure(View.java:17547)
    at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1676)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
    at android.view.Choreographer.doFrame(Choreographer.java:550)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)


at com.andro
02-21 14:14:29.702 1707-1707/com.android.inputmethod.latin E/RichInputConnection: Unable to connect to the editor to retrieve text.
02-21 14:14:31.705 1707-1707/com.android.inputmethod.latin E/RichInputConnection: Unable to connect to the editor to retrieve text.
02-21 14:14:33.706 1707-1707/com.android.inputmethod.latin E/RichInputConnection: Unable to connect to the editor to retrieve text.
02-21 14:14:35.709 1707-1707/com.android.inputmethod.latin E/RichInputConnection: Unable to connect to the editor to retrieve text.

我不确定是什么问题,我正在以完全相同的方式将文本设置为TextView ,并且它工作正常。 我查看了论坛和文章,我看到有类似问题的人,但似乎他们要么没有正确地绑定视图与我的ViewHolder属性或试图访问

这是我的自定义Adapter类:

public class ProductListAdapter extends RecyclerView.Adapter<ProductListViewHolder>{
    private List<Product> productList;
    private ProductItemManager productItemManager;

    public ProductListAdapter(List<Product> productList, ProductItemManager productItemManager) {
        this.productList = productList;
        this.productItemManager = productItemManager;
    }

    @NonNull
    @Override
    public ProductListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View productListView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_product_list, parent, false);
        return new ProductListViewHolder(productListView, productItemManager);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductListViewHolder productListViewHolder, final int position) {
        Product product = productList.get(position);

        Double finalPrice = product.getSellingPrice() * product.getQuantity();

        productListViewHolder.productDescription.setText(product.getDescription());
        productListViewHolder.productSellingPrice.setText("un. " + Utils.doubleToReal(product.getSellingPrice()));
        productListViewHolder.productFinalPrice.setText(Utils.doubleToReal(finalPrice));
        productListViewHolder.productQuantity.setText(Double.toString(product.getQuantity()));
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }
}

这是我的ViewHolder

public class ProductListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView productDescription, productSellingPrice, productFinalPrice;
    public ImageView removeProductIcon;
    public EditText productQuantity;
    private ProductItemManager productItemManager;

    public ProductListViewHolder(View itemView, ProductItemManager productItemManager) {
        super(itemView);

        productDescription = itemView.findViewById(R.id.productDescription);
        productSellingPrice = itemView.findViewById(R.id.productSellingPrice);
        productFinalPrice = itemView.findViewById(R.id.productFinalPrice);
        removeProductIcon = itemView.findViewById(R.id.removeProductIcon);
        productQuantity = itemView.findViewById(R.id.productQuantity);

        this.productItemManager = productItemManager;

        removeProductIcon.setOnClickListener(this);

        this.productChangeListener();
    }

    private void productChangeListener() {
        productQuantity.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Double quantity = Double.parseDouble(productQuantity.getText().toString());
                productItemManager.setProductQuantity(getAdapterPosition(), quantity);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        productItemManager.removeProduct(getAdapterPosition());
    }
}

一旦查看绑定,就应该在TextWatch上调用侦听器集。 onBindViewHolder调用您的注册。

......
@Override
public void onBindViewHolder(@NonNull ProductListViewHolder productListViewHolder, final int position) {
......
productListViewHolder.productChangeListener();
....

我还建议改变注册监听器的功能。 在创建RecyclerView期间,无需创建数百个项目。

有一些事情需要你注意:

  1. 由于他们的用户体验,在RecyclerViews(以及一般列表)中使用EditText通常不是一个好主意。 您必须阻止用户滚动列表,或者根据滚动执行某些操作,以使事情更顺畅(因为他们可以正在输入内容)。

  2. 代码中有一个非常重要的问题,它是对正在回收的编辑文本的“TextWatcher”的引用。 您需要在回收视图时删除文本观察程序。 如果您不删除TextWatcher,会发生什么? 由于它不像OnClickListener (它不是一个set函数而且是一个add函数),每次重新创建单元格时,一个新的文本观察器会被添加到EditText中,并引用旧的ViewHolder。

要解决此问题,您需要向ViewHolder添加一个方法来取消绑定它所拥有的任何内容:

public class ProductListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView productDescription, productSellingPrice, productFinalPrice;
        public ImageView removeProductIcon;
        public EditText productQuantity;
        private ProductItemManager productItemManager;
        private TextWatcher textWatcher;

        public ProductListViewHolder(View itemView, ProductItemManager productItemManager) {
            super(itemView);

            productDescription = itemView.findViewById(R.id.productDescription);
            productSellingPrice = itemView.findViewById(R.id.productSellingPrice);
            productFinalPrice = itemView.findViewById(R.id.productFinalPrice);
            removeProductIcon = itemView.findViewById(R.id.removeProductIcon);
            productQuantity = itemView.findViewById(R.id.productQuantity);

            this.productItemManager = productItemManager;

            removeProductIcon.setOnClickListener(this);

            this.productChangeListener();
        }

        private void productChangeListener() {
            textWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    Double quantity = Double.parseDouble(productQuantity.getText().toString());
                    productItemManager.setProductQuantity(getAdapterPosition(), quantity);
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            };
            productQuantity.addTextChangedListener(textWatcher);
        }

        public void unbind(){
            productQuantity.removeTextChangedListener(textWatcher);
        }
        @Override
        public void onClick(View v) {
            productItemManager.removeProduct(getAdapterPosition());
        }
    }

然后,您可以使用RecyclerView的onViewRecycled方法来了解您的视图何时被回收并删除TextWatcher:

@Override
    public void onViewRecycled(ProductListViewHolder productListViewHolder) {
        super.onViewRecycled(holder);
        productListViewHolder.unbind();
    }

每次观察的文本发生变化时,都会调用TextWatcher 这不仅意味着用户编辑将调用观察者(好),但每次视图持有者反弹时,观察者也将被调用(不太好)。 这也意味着将不必要地调用productItemManager.setProductQuantity() (我说“不必要”但是,也许,这是你打算发生的事情。)

绑定视图持有者时避免文本观察者的一种方法是在View.OnFocusChangeListener()上设置View.OnFocusChangeListener() ,该productQuantity将应用并删除文本观察者,因为该字段会获得或丢失焦点。 换句话说,文本观察器仅在编辑EditText时才处于活动状态。

productQuantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            productQuantity.addTextChangedListener(textWatcher);
        } else {
            productQuantity.removeTextChangedListener(textWatcher);
        }
    }
});

这可能无法解决您遇到的问题,但它将消除可能导致因素的不必要工作。

我认为发布setProductQuantity()将帮助某人找到问题的根源。

我想同意@Cheticamp提出的实现。 根据我的经验,文本更改侦听器可能会在不应该被调用的不同情况下触发。 EditText通常涉及自动更正单词和提供建议,并且可以在初始化期间调用。 因此,我建议您将以下内容放在布局文件中的EditText声明中。

android:inputType="textNoSuggestions"

而不是使用setOnFocusChangeListener您可能会考虑在文本观察器中进行null检查,如下所示。

private void productChangeListener() {
    productQuantity.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String editTextContent = productQuantity.getText().toString();
            if(editTextContent != null && editTextContent.trim().length() > 0) {
                Double quantity = Double.parseDouble(productQuantity.getText().toString());
                productItemManager.setProductQuantity(getAdapterPosition(), quantity);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

希望有所帮助!

 //update this line in your code, you will get your answer :) 

productListViewHolder.productQuantity.setText(Double.toString(productList.get(position).getQuantity()));
  相关解决方案