Mistake at RecyclerView



  • When I try to add a paragraph RecyclerView Like this. AsyncTask😞

    //mListFormOne — лист, который передается в адаптер
    //mAdapter — адаптер, подключенный к RecyclerView
    //record — новая запись для добавления в RecyclerView
    

    mListFormOne.add(record);
    mAdapter.notifyItemInserted(mListFormOne.size() - 1);

    Once again, he makes a mistake:

    Caused by: java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
    at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2042)
    at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onItemRangeInserted(RecyclerView.java:4129)
    at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyItemRangeInserted(RecyclerView.java:9302)
    at android.support.v7.widget.RecyclerView$Adapter.notifyItemInserted(RecyclerView.java:5662)
    at com.dugin_rostislav.dictionary.fragments.PageWords.addRecordFormOne(PageWords.java:60)
    at com.dugin_rostislav.dictionary.MainActivity$LoadData.doInBackground(MainActivity.java:289)
    at com.dugin_rostislav.dictionary.MainActivity$LoadData.doInBackground(MainActivity.java:207)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
                at java.lang.Thread.run(Thread.java:856)

    How do you fix that?


    UPD_0:
    Yuri,

    mListFormTwo.add(record);

    Handler handler = new Handler();

    final Runnable r = new Runnable() {
    public void run() {
    mAdapter.notifyDataSetChanged();
    }
    };

    handler.post(r);

    Mistake:

     java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:304)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.dugin_rostislav.dictionary.adapters.RecyclerWordsAdapter.notifyItemInserted(int)' on a null object reference
    at com.dugin_rostislav.dictionary.fragments.PageWords.addRecordFormOne(PageWords.java:58)
    at com.dugin_rostislav.dictionary.MainActivity$LoadData.doInBackground(MainActivity.java:289)
    at com.dugin_rostislav.dictionary.MainActivity$LoadData.doInBackground(MainActivity.java:207)
    at android.os.AsyncTask$2.call(AsyncTask.java:292)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                at java.lang.Thread.run(Thread.java:818)



  • According to the https://stackoverflow.com/a/32644466/3212712 the adapter is being renovated with help. Handler:

    Handler handler = new Handler();
    

    final Runnable r = new Runnable() {
    public void run() {
    mAdapter.notifyItemInserted(mListFormOne.size() - 1);
    }
    };

    handler.post(r);

    On the en-SO, they're trying to explain it to the anglo-saxons and suggest another. https://stackoverflow.com/a/31069171/3212712 ♪




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2