jiang99357 2023-02-23 22:35 采纳率: 100%
浏览 126
已结题

android studio sqlite异常 java.util.ConcurrentModificationException异常

java.util.ConcurrentModificationException异常
刚刚转战Android,花了一个星期弄了个东西,

一直正常编译运行,买包烟回来就不能运行了,买烟之前都还好好的,
有备份,运行备份也是一样
求帮忙。

img



public class MainActivity2 extends AppCompatActivity {
    DBAdapter dbAdapter;
    TextView text;
    FloatingActionButton fab;




    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main2);
        dbAdapter=new DBAdapter(this);
        text = findViewById(R.id.textView);
        fab = findViewById(R.id.floatingActionButton);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//点击悬浮按钮跳转到添加界面
                Intent intent = new Intent(MainActivity2.this,MainActivity.class);
                startActivity(intent);

            }
        });

        List<MyFruit> mFruitList = new ArrayList<MyFruit>();
        MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(this,DB_NAME,null,1);
        SQLiteDatabase sqLiteDatabase=mySQLiteOpenHelper.getWritableDatabase();
        Cursor cursor=sqLiteDatabase.query(DB_TABLE,null,null,null,null,null,"_id DESC");
        while (cursor.moveToNext()){//报错行
            String _id = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
            String khmc = cursor.getString(cursor.getColumnIndexOrThrow("khmc"));
            String gcmc=cursor.getString(cursor.getColumnIndexOrThrow("gcmc"));
            String sgbw=cursor.getString(cursor.getColumnIndexOrThrow("sgbw"));
            String ysrq=cursor.getString(cursor.getColumnIndexOrThrow("ysrq"));
            String qddj=cursor.getString(cursor.getColumnIndexOrThrow("qddj"));
            String ksdj=cursor.getString(cursor.getColumnIndexOrThrow("ksdj"));
            String bcfl=cursor.getString(cursor.getColumnIndexOrThrow("bcfl"));
            String ljfl=cursor.getString(cursor.getColumnIndexOrThrow("ljfl"));
            String ysch=cursor.getString(cursor.getColumnIndexOrThrow("ysch"));
            String jsy=cursor.getString(cursor.getColumnIndexOrThrow("jsy"));

            MyFruit bookStore=new MyFruit(_id,khmc,gcmc,sgbw,ysrq,qddj,ksdj,bcfl,ljfl,ysch,jsy);
            mFruitList.add(bookStore);

        }
        text.setText("总车数:"+allCaseNum()+"        "+"累计方量:"+(int) getsumdebt()+"M³");
        cursor.close();

        // 获取到 RecyclerView 实例
        RecyclerView mRecyclerView = findViewById(R.id.test_rv);
        // 创建 LinearLayoutManager 对象 ,即线性布局
        LinearLayoutManager mManager = new LinearLayoutManager(this);

        mRecyclerView.setLayoutManager(mManager);
        // 创建自定义的 Adapter 实例
        MyFruitAdapter mAdapter = new MyFruitAdapter(mFruitList);
        //进场动画
        LayoutAnimationController controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this,R.anim.my_anim));
        mRecyclerView.setLayoutAnimation(controller);
        // 设置适配器
        mRecyclerView.setAdapter(mAdapter);
        //itme 点击事件
        mAdapter.setOnItemClick(new MyFruitAdapter.OnItemClick() {
            @Override
            public void onItemClick(int position) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity2.this);
                dialog.setTitle("请选择操作:");
                dialog.setIcon(R.drawable.icon);
                dialog.setPositiveButton("修改",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(MainActivity2.this,Update.class);
                                MyFruit myFruit  = mFruitList.get(position);
                                intent.putExtra("_id",myFruit.get_id());
                                intent.putExtra("khmc",myFruit.getKhmc());
                                intent.putExtra("gcmc",myFruit.getGcmc());
                                intent.putExtra("sgbw",myFruit.getSgbw());
                                intent.putExtra("ysrq",myFruit.getYsrq());
                                intent.putExtra("qddj",myFruit.getQddj());
                                intent.putExtra("ksdj",myFruit.getKsdj());
                                intent.putExtra("bcfl",myFruit.getBcfl());
                                intent.putExtra("ljfl",myFruit.getLjfl());
                                intent.putExtra("ysch",myFruit.getYsch());
                                intent.putExtra("jsy",myFruit.getJsy());
                                startActivity(intent);
                                }
                            });
                dialog.setNeutralButton("删除",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                MyFruit myFruit  = mFruitList.get(position);
                                String id1 = myFruit.get_id();
                                boolean shanchu = deleteUser(id1);
                                if (shanchu == true){
                                    mFruitList.remove(position);
                                    mAdapter.notifyItemRemoved(position);
                                    mAdapter.notifyItemRangeChanged(position, mFruitList.size());
                                    Toast.makeText(MainActivity2.this,"删除成功!",Toast.LENGTH_SHORT).show();

                                }else {Toast.makeText(MainActivity2.this,"删除失败!!!",Toast.LENGTH_SHORT).show();}
                                    }
                                });
                dialog.setNegativeButton("复制", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(MainActivity2.this,Copyone.class);
                        MyFruit myFruit  = mFruitList.get(position);
                        intent.putExtra("_id",myFruit.get_id());
                        intent.putExtra("khmc",myFruit.getKhmc());
                        intent.putExtra("gcmc",myFruit.getGcmc());
                        intent.putExtra("sgbw",myFruit.getSgbw());
                        intent.putExtra("ysrq",myFruit.getYsrq());
                        intent.putExtra("qddj",myFruit.getQddj());
                        intent.putExtra("ksdj",myFruit.getKsdj());
                        intent.putExtra("bcfl",myFruit.getBcfl());
                        intent.putExtra("ljfl",myFruit.getLjfl());
                        intent.putExtra("ysch",myFruit.getYsch());
                        intent.putExtra("jsy",myFruit.getJsy());
                        startActivity(intent);

                    }
                });
                dialog.create();
                dialog.show();
            }
        });
    }




    public double getsumdebt() {//统计总方量
        int total = 0;
        MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(this,DB_NAME,null,1);
        SQLiteDatabase sqLiteDatabase=mySQLiteOpenHelper.getWritableDatabase();
        Cursor mCursor = sqLiteDatabase.rawQuery(" SELECT SUM(bcfl) FROM peopleinfo", null);
        if (mCursor.moveToFirst())
        {
            total = mCursor.getInt(0);
        }
        while (mCursor.moveToNext());
        return total;
    }
    /**
     * 查询数据库中的总条数.
     */
    public long allCaseNum( ){
        MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(MainActivity2.this,DB_NAME,null,1);
        SQLiteDatabase sqLiteDatabase=mySQLiteOpenHelper.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("select count(*) from "+DB_TABLE, null);
        cursor.moveToFirst();
        long count = cursor.getLong(0);
        return count;
    }
    public boolean deleteUser(String  ID){//删除方法

        MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(this,DB_NAME,null,1);
        SQLiteDatabase sqLiteDatabase=mySQLiteOpenHelper.getWritableDatabase();
        long id = sqLiteDatabase.delete(DB_TABLE,KEY_ID+"="+ID,null);
        sqLiteDatabase.close();
        return id>0?true:false;

    }
    public long update (String id ,ContentValues updateValues){

        MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(this,DB_NAME,null,1);
        SQLiteDatabase sqLiteDatabase=mySQLiteOpenHelper.getWritableDatabase();
        sqLiteDatabase.update(DB_TABLE,updateValues,KEY_ID+"="+id,null);
        return 0;
    }

}

  • 写回答

9条回答 默认 最新

  • 「已注销」 2023-02-24 07:20
    关注

    参考GPT和自己的思路,该异常java.util.ConcurrentModificationException表示在迭代列表时,另一个线程修改了该列表,导致迭代器失效。在你的代码中没有明显的多线程,所以可能是因为在迭代mFruitList时在另一个地方修改了mFruitList。可能的原因是你在使用mFruitList的同时,在另一个线程中对mFruitList进行了修改。可以尝试在对mFruitList进行迭代的时候对其进行同步,以防止其他线程同时对其进行修改,如下所示:

    List<MyFruit> mFruitList = new ArrayList<MyFruit>();
    MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(this,DB_NAME,null,1);
    SQLiteDatabase sqLiteDatabase=mySQLiteOpenHelper.getWritableDatabase();
    Cursor cursor=sqLiteDatabase.query(DB_TABLE,null,null,null,null,null,"_id DESC");
    
    synchronized (mFruitList) { // 同步块
        while (cursor.moveToNext()){                   
            String _id = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
            String khmc = cursor.getString(cursor.getColumnIndexOrThrow("khmc"));
            String gcmc=cursor.getString(cursor.getColumnIndexOrThrow("gcmc"));
            String sgbw=cursor.getString(cursor.getColumnIndexOrThrow("sgbw"));
            String ysrq=cursor.getString(cursor.getColumnIndexOrThrow("ysrq"));
            String qddj=cursor.getString(cursor.getColumnIndexOrThrow("qddj"));
            String ksdj=cursor.getString(cursor.getColumnIndexOrThrow("ksdj"));
            String bcfl=cursor.getString(cursor.getColumnIndexOrThrow("bcfl"));
            String ljfl=cursor.getString(cursor.getColumnIndexOrThrow("ljfl"));
            String ysch=cursor.getString(cursor.getColumnIndexOrThrow("ysch"));
            String jsy=cursor.getString(cursor.getColumnIndexOrThrow("jsy"));
    
            MyFruit bookStore=new MyFruit(_id,khmc,gcmc,sgbw,ysrq,qddj,ksdj,bcfl,ljfl,ysch,jsy);
    
            mFruitList.add(bookStore);
        }
    }
    cursor.close();
    

    另外,也可以尝试使用CopyOnWriteArrayList类代替ArrayList类,CopyOnWriteArrayList类是一个线程安全的ArrayList,当有修改操作时,它会创建一个新的副本来进行修改,而不会影响到迭代操作,从而避免了ConcurrentModificationException异常。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

问题事件

  • 系统已结题 3月10日
  • 已采纳回答 3月2日
  • 修改了问题 2月24日
  • 修改了问题 2月24日
  • 展开全部

悬赏问题

  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振