Java 在RecyclerView';中将文本设置为EditText时出错;s onBindViewHolder方法

Java 在RecyclerView';中将文本设置为EditText时出错;s onBindViewHolder方法,java,android,android-recyclerview,android-arrayadapter,Java,Android,Android Recyclerview,Android Arrayadapter,我试图在自定义适配器内将文本设置为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.

我试图在自定义适配器内将文本设置为
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
,效果很好。我浏览过论坛和文章,也看到过有类似问题的人,但他们似乎没有将视图与I的
ViewHolder
属性正确绑定,或者试图访问

这是我的自定义适配器类:

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();
    }
}

查看绑定后,应调用TextWatch上的侦听器集。在
onBindViewHolder
中,呼叫您的注册

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

我还建议更改注册侦听器的函数。在
RecyclerView
创建期间,不创建数百个项目

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

绑定视图持有者时避免文本观察者的一种方法是在
productQuantity
上设置
view.OnFocusChangeListener()。换句话说,文本观察程序仅在编辑
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"

而不是使用<代码> StEnFoCuxCuxelister-< /C> >您可以考虑使用<代码> null >代码>检查您的文本监视器,如下所示。p>

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) {

        }
    });
}

希望有帮助

有几件事需要你注意:

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

  • 代码中有一个非常重要的问题,那就是引用正在回收的编辑文本的“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的
    onViewRecyclered
    方法来了解视图何时被回收,并删除TextWatcher:

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

    可能重复完整堆栈跟踪后不仅错误我编辑了添加完整堆栈的问题。因此我刚刚测试了应用程序,发现问题似乎只有在ProductListViewHolder的构造函数中有TextWatcher侦听器时才会出现。如果您能想到这一点,我将不胜感激。如果您发布ProductItemManager类代码,那将非常有帮助,因为这就是这个bug的来源。根据我读到的一篇文章,在视图持有者之外声明侦听器是一种不好的做法。这是真的吗?作者建议在viewholder上实现监听器,然后创建一个我自己的接口,该接口将由我的活动扩展,在那里我的接口的方法应该被覆盖。这就是我在点击事件中对ViewHolder所做的,效果很好。你能进一步解释一下吗?原来的线路出了什么问题,你做了什么使它工作?请记住,OP应该能够从您的改进中学习,而不是简单地复制它。最近我遇到了同样的问题,我通过直接访问主列表位置解决了它。请将所有解释添加到答案中,而不是添加到评论部分。此外,请更深入地解释您所做的更改及其原因-原始代码有什么问题?
    @Override
        public void onViewRecycled(ProductListViewHolder productListViewHolder) {
            super.onViewRecycled(holder);
            productListViewHolder.unbind();
        }
    
     //update this line in your code, you will get your answer :) 
    
    productListViewHolder.productQuantity.setText(Double.toString(productList.get(position).getQuantity()));