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

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 MATLAB动图输出问题
  • ¥50 python如何用抖音这个接口输入抖音ID查询用户信息
  • ¥30 关于移动Web网页使用TinyMCE富文本编辑器上传图片后的光标定位、压缩等几个问题如何解决:
  • ¥25 activeMq在同服务器centos8下消费很慢
  • ¥20 为什么zynq CAN IP 无法进入config配置模式,XCan_SelfTest函数失效?
  • ¥15 Pycharm中程序直接运行可以但进入调试报错
  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥15 我的显卡支持CUDA最高版本是12.3,这个版本也支持VS 2022 17.0这种情况下如果我想下载CUDA11.8,需要下载旧版本的VS2022吗
  • ¥15 pointnet2包安装