java.util.ConcurrentModificationException异常
刚刚转战Android,花了一个星期弄了个东西,
一直正常编译运行,买包烟回来就不能运行了,买烟之前都还好好的,
有备份,运行备份也是一样
求帮忙。
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;
}
}