IllegalStateException - 如何修复?

时间:2017-12-29 20:06:43

标签: android

我制作了Custom RecyclerView,其自定义适配器是:

package com.vrishankgupta.todolistfile.Model;


public class CourseAdapter extends     
RecyclerView.Adapter<CourseAdapter.CourseViewHolder> {

Context context;
ArrayList<listClass> tasks;
public CourseAdapter(Context context, ArrayList<listClass> tasks) {
    this.context = context;
    this.tasks = tasks;
}

@Override
public CourseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = li.inflate(viewType,parent,false);

    return new CourseViewHolder(itemView);
}

@Override
public int getItemViewType(int position) {
    return R.layout.detail;
}

@Override
public void onBindViewHolder(final CourseViewHolder holder, int position) {
    final listClass task = tasks.get(position);

    holder.tv.setText(task.getTask());
    holder.cb.setChecked(task.isActive());
    holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            task.setActive(holder.cb.isChecked());
            notifyDataSetChanged();
            new CourseAdapter(context,tasks);
//                Toast.makeText(,task.getTask() + " is " +task.isActive(),Toast.LENGTH_SHORT).show();
        }
    });

    holder.delCb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tasks.remove(task);
            notifyDataSetChanged();
            new CourseAdapter(context,tasks);
        }
    });
}

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

static class CourseViewHolder extends RecyclerView.ViewHolder
{
    TextView tv;
    CheckBox cb;
    ImageButton delCb;

    public CourseViewHolder(View converView) {
        super(converView);
        tv = converView.findViewById(R.id.listtv);
        cb = converView.findViewById(R.id.activeCb);
        delCb = converView.findViewById(R.id.delCb);
    }
}
}

每当我按下CheckBox或删除按钮时,App都会崩溃并显示以下日志:

FATAL EXCEPTION: main  Process: com.vrishankgupta.todolistfile, PID: 4993         java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling android.support.v7.widget.RecyclerView{99a7f9f VFED..... ......ID 0,270-1080,1536 #7f070058 app:id/recycle}, adapter:com.vrishankgupta.todolistfile.Model.CourseAdapter@da4f8ec, layout:android.support.v7.widget.LinearLayoutManager@59e14b5, context:com.vrishankgupta.todolistfile.RecyclerActivity@d0a1312        at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2663)
  at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:5055)
  at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11540)
  at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:6762)
  at com.vrishankgupta.todolistfile.Model.CourseAdapter$1.onCheckedChanged(CourseAdapter.java:51)
  at android.widget.CompoundButton.setChecked(CompoundButton.java:156)
  at com.vrishankgupta.todolistfile.Model.CourseAdapter.onBindViewHolder(CourseAdapter.java:46)
  at com.vrishankgupta.todolistfile.Model.CourseAdapter.onBindViewHolder(CourseAdapter.java:19)
  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
  at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
  at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
  at android.view.View.layout(View.java:17653)
  at android.view.ViewGroup.layout(ViewGroup.java:5579)
  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
  at android.view.View.layout(View.java:17653)
  at android.view.ViewGroup.layout(ViewGroup.java:5579)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
  at android.view.View.layout(View.java:17653)
  at android.view.ViewGroup.layout(ViewGroup.java:5579)
  at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
  at android.view.View.layout(View.java:17653)
  at android.view.ViewGroup.layout(ViewGroup.java:5579)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
  at android.view.View.layout(View.java:17653)
  at android.view.ViewGroup.layout(ViewGroup.java:5579)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
  at android.view.View.layout(View.java:17653)
  at android.view.ViewGroup.layout(ViewGroup.java:5579)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
  at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
  at android.view.View.layout(View.java:17653)
  at android.view.ViewGroup.layout(ViewGroup.java:5579) 12-29 14:51:36.386 4993-4993/com.vrishankgupta.todolistfile E/AndroidRuntime:     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2364)
  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2086)
  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1272)
  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6408)
  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
  at android.view.Choreographer.doCallbacks(Choreographer.java:686)
  at android.view.Choreographer.doFrame(Choreographer.java:621)
  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
  at android.os.Handler.handleCallback(Handler.java:751)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6165)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

我的主要活动(如果需要)

public class RecyclerActivity extends AppCompatActivity {
public static final String TAG = "ERROR";

ArrayList<listClass> arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recycler);
    final Button addButn = findViewById(R.id.addButn);
    final EditText etnew = findViewById(R.id.etnew);


    final RecyclerView rv = findViewById(R.id.recycle);

    int perm = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);

    if(perm == PackageManager.PERMISSION_GRANTED)
    {
        arr = (ArrayList<listClass>) readFile();
    }
    else
    {
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},45);
    }

    final CourseAdapter adapter = new CourseAdapter(this,arr);
    rv.setLayoutManager(new LinearLayoutManager(this));
    rv.setAdapter(adapter);

    addButn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(etnew.getText() == null || etnew.getText().toString().equals(""))
            {
                Toast.makeText(getApplicationContext(),"Enter Some Data",Toast.LENGTH_SHORT).show();
            }
            else {
                listClass task = new listClass(etnew.getText().toString(), false);
                arr.add(task);
                etnew.setText("");
                rv.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

        }
    });

}



void writeFile(ArrayList<listClass> arr)
{
    File sdCard = Environment.getExternalStorageDirectory();
    File taskFile = new File(sdCard,"taskFile.txt");
    try {
        FileOutputStream fout = new FileOutputStream(taskFile,false);
        ObjectOutputStream objOut = new ObjectOutputStream(fout);
        objOut.writeObject(arr);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Object readFile()
{
    boolean cont = true;
    try {
        File sdCard = Environment.getExternalStorageDirectory();
        File task = new File(sdCard,"taskFile.txt");
        FileInputStream fin = new FileInputStream(task);
        ObjectInputStream objIn = new ObjectInputStream(fin);

        while (cont)
        {
            Object t = objIn.readObject();
            if(t!=null)
                return t;
            else cont = false;
        }

    }catch (Exception e)
    {
        Log.e(TAG, "readFile: " );
    }

    return  (Object)new ArrayList<listClass>();
}

@Override
protected void onStop() {
    super.onStop();

    int perm = ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    );

    if(perm == PackageManager.PERMISSION_GRANTED)
    {
        writeFile(arr);

    } else{
        ActivityCompat.requestPermissions(
                this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                44
        );
    }

}
}

我认为我的CheckBox和删除按钮的OnClickListener存在问题,但无法解决!

0 个答案:

没有答案