问题:当我从activitycart移动到另一个页面(添加项目后)并再次回到Mycart并删除该项目时,我的应用程序崩溃,并在下面给出错误。据我所知,当我离开Activitycart时,它会关闭db_order,当我返回并删除任何内容时,它会尝试对已经关闭的数据库执行操作。此外它还给出了asyntask错误。但我是android的新手并且不知道如何&在哪里修复错误。请帮忙。
错误:
DB ERROR: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/twik.in/databases/db_order
W/System.err: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/twik.in/databases/db_order
W/System.err:at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
W/System.err:at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1494)
W/System.err:at twik.in.DBHelper.deleteAllData(DBHelper.java:230)
W/System.err:at twik.in.ActivityCart$4.onClick(ActivityCart.java:212)
W/System.err:at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
W/System.err:at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:at android.os.Looper.loop(Looper.java:148)
W/System.err:at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err:at java.lang.reflect.Method.invoke(Native Method)
W/System.err:at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: twik.in, PID: 5477
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/twik.in/databases/db_order
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1158)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at twik.in.DBHelper.getAllData(DBHelper.java:123)
at twik.in.ActivityCart.getDataFromDatabase(ActivityCart.java:378)
at twik.in.ActivityCart$getDataTask.doInBackground(ActivityCart.java:350)
at twik.in.ActivityCart$getDataTask.doInBackground(ActivityCart.java:335)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Activitycart.java
package twik.in;
public class ActivityCart extends Activity {
// declare view objects
// ImageButton imgNavBack;
ListView listOrder;
ProgressBar prgLoading;
TextView txtTotalLabel, txtTotal, txtAlert;
Button btnClear, Checkout;
RelativeLayout lytOrder;
// declate dbhelper and adapter objects
DBHelper dbhelper;
AdapterCart mola;
// declare static variables to store tax and currency data
static double Tax;
static String Currency;
// declare arraylist variable to store data
ArrayList<ArrayList<Object>> data;
static ArrayList<Integer> Menu_ID = new ArrayList<Integer>();
static ArrayList<String> Menu_name = new ArrayList<String>();
static ArrayList<Integer> Quantity = new ArrayList<Integer>();
static ArrayList<Double> Sub_total_price = new ArrayList<Double>();
double Total_price;
final int CLEAR_ALL_ORDER = 0;
final int CLEAR_ONE_ORDER = 1;
int FLAG;
int ID;
String TaxCurrencyAPI;
int IOConnect = 0;
// create price format
DecimalFormat formatData = new DecimalFormat("#.##");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_order);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.header)));
bar.setTitle("Order Detail");
bar.setDisplayHomeAsUpEnabled(true);
bar.setHomeButtonEnabled(true);
// connect view objects with xml id
// imgNavBack = (ImageButton) findViewById(R.id.imgNavBack);
Checkout = (Button) findViewById(R.id.Checkout);
prgLoading = (ProgressBar) findViewById(R.id.prgLoading);
listOrder = (ListView) findViewById(R.id.listOrder);
txtTotalLabel = (TextView) findViewById(R.id.txtTotalLabel);
txtTotal = (TextView) findViewById(R.id.txtTotal);
txtAlert = (TextView) findViewById(R.id.txtAlert);
btnClear = (Button) findViewById(R.id.btnClear);
lytOrder = (RelativeLayout) findViewById(R.id.lytOrder);
// tax and currency API url
TaxCurrencyAPI = Constant.TaxCurrencyAPI+"?accesskey="+Constant.AccessKey;
mola = new AdapterCart(this);
dbhelper = new DBHelper(this);
// open database
try{
dbhelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
// call asynctask class to request tax and currency data from server
new getTaxCurrency().execute();
// event listener to handle clear button when clicked
btnClear.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
// show confirmation dialog
showClearDialog(CLEAR_ALL_ORDER, 1111);
}
});
// event listener to handle list when clicked
listOrder.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// show confirmation dialog
showClearDialog(CLEAR_ONE_ORDER, Menu_ID.get(position));
}
});
// event listener to handle back button when clicked
//imgNavBack.setOnClickListener(new OnClickListener() {
//public void onClick(View arg0) {
// // TODO Auto-generated method stub
// // close database and back to previous page
// dbhelper.close();
// finish();
// overridePendingTransition(R.anim.open_main, R.anim.close_next);
// }
// });
Checkout.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
// close database and back to previous page
dbhelper.close();
Intent iReservation = new Intent(ActivityCart.this, ActivityCheckout.class);
startActivity(iReservation);
overridePendingTransition(R.anim.open_next, R.anim.close_next);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
this.finish();
overridePendingTransition(R.anim.open_main, R.anim.close_next);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// method to create dialog
void showClearDialog(int flag, int id){
FLAG = flag;
ID = id;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.confirm);
switch(FLAG){
case 0:
builder.setMessage(getString(R.string.clear_all_order));
break;
case 1:
builder.setMessage(getString(R.string.clear_one_order));
break;
}
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch(FLAG){
case 0:
// clear all menu in order table
dbhelper.deleteAllData();
listOrder.invalidateViews();
clearData();
new getDataTask().execute();
break;
case 1:
// clear selected menu in order table
dbhelper.deleteData(ID);
listOrder.invalidateViews();
clearData();
new getDataTask().execute();
break;
}
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// close dialog
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
// asynctask class to handle parsing json in background
public class getTaxCurrency extends AsyncTask<Void, Void, Void>{
// show progressbar first
getTaxCurrency(){
if(!prgLoading.isShown()){
prgLoading.setVisibility(0);
txtAlert.setVisibility(8);
}
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
// parse json data from server in background
parseJSONDataTax();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
// when finish parsing, hide progressbar
prgLoading.setVisibility(8);
// if internet connection available request data form server
// otherwise, show alert text
if(IOConnect == 0){
new getDataTask().execute();
}else{
txtAlert.setVisibility(0);
txtAlert.setText(R.string.alert);
}
}
}
// method to parse json data from server
public void parseJSONDataTax(){
try {
// request data from tax and currency API
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
HttpUriRequest request = new HttpGet(TaxCurrencyAPI);
HttpResponse response = client.execute(request);
InputStream atomInputStream = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(atomInputStream));
String line;
String str = "";
while ((line = in.readLine()) != null){
str += line;
}
// parse json data and store into tax and currency variables
JSONObject json = new JSONObject(str);
JSONArray data = json.getJSONArray("data"); // this is the "items: [ ] part
JSONObject object_tax = data.getJSONObject(0);
JSONObject tax = object_tax.getJSONObject("tax_n_currency");
Tax = Double.parseDouble(tax.getString("Value"));
JSONObject object_currency = data.getJSONObject(1);
JSONObject currency = object_currency.getJSONObject("tax_n_currency");
Currency = currency.getString("Value");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
IOConnect = 1;
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// clear arraylist variables before used
void clearData(){
Menu_ID.clear();
Menu_name.clear();
Quantity.clear();
Sub_total_price.clear();
}
// asynctask class to handle parsing json in background
public class getDataTask extends AsyncTask<Void, Void, Void>{
// show progressbar first
getDataTask(){
if(!prgLoading.isShown()){
prgLoading.setVisibility(0);
lytOrder.setVisibility(8);
txtAlert.setVisibility(8);
}
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
// get data from database
getDataFromDatabase();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
// show data
txtTotal.setText( Total_price+" "+Currency);
txtTotalLabel.setText(getString(R.string.total_order)+" ("+Tax+"% XTRAOFF)");
prgLoading.setVisibility(8);
// if data available show data on list
// otherwise, show alert text
if(Menu_ID.size() > 0){
lytOrder.setVisibility(0);
listOrder.setAdapter(mola);
}else{
txtAlert.setVisibility(0);
}
}
}
// method to get data from server
public void getDataFromDatabase(){
Total_price = 0;
clearData();
data = dbhelper.getAllData();
// store data to arraylist variables
for(int i=0;i<data.size();i++){
ArrayList<Object> row = data.get(i);
Menu_ID.add(Integer.parseInt(row.get(0).toString()));
Menu_name.add(row.get(1).toString());
Quantity.add(Integer.parseInt(row.get(2).toString()));
Sub_total_price.add(Double.parseDouble(formatData.format(Double.parseDouble(row.get(3).toString()))));
Total_price += Sub_total_price.get(i);
}
// count total order
Total_price -= (Total_price * (Tax/100));
Total_price = Double.parseDouble(formatData.format(Total_price));
}
// when back button pressed close database and back to previous page
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
dbhelper.close();
finish();
overridePendingTransition(R.anim.open_main, R.anim.close_next);
}
@Override
public void onConfigurationChanged(final Configuration newConfig)
{
// Ignore orientation change to keep activity from restarting
super.onConfigurationChanged(newConfig);
}}
DBhelper.java
public class DBHelper extends SQLiteOpenHelper{
String DB_PATH;
private final static String DB_NAME = "db_order";
public final static int DB_VERSION = 1;
public static SQLiteDatabase db;
private final Context context;
private final String TABLE_NAME = "tbl_order";
private final String ID = "id";
private final String MENU_NAME = "Menu_name";
private final String QUANTITY = "Quantity";
private final String TOTAL_PRICE = "Total_price";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
DB_PATH = Constant.DBPath;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
SQLiteDatabase db_Read = null;
if(dbExist){
//do nothing - database already exist
}else{
db_Read = this.getReadableDatabase();
db_Read.close();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException{
InputStream myInput = context.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public void close() {
if(db!=null)
db.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/** this code is used to get all data from database */
public ArrayList<ArrayList<Object>> getAllData(){
ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
Cursor cursor = null;
try{
cursor = db.query(
TABLE_NAME,
new String[]{ID, MENU_NAME, QUANTITY, TOTAL_PRICE},
null,null, null, null, null);
cursor.moveToFirst();
if (!cursor.isAfterLast()){
do{
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
dataList.add(cursor.getString(3));
dataArrays.add(dataList);
}
while (cursor.moveToNext());
}
cursor.close();
}catch (SQLException e){
Log.e("DB Error", e.toString());
e.printStackTrace();
}
return dataArrays;
}
/** this code is used to get all data from database */
public boolean isDataExist(long id){
boolean exist = false;
Cursor cursor = null;
try{
cursor = db.query(
TABLE_NAME,
new String[]{ID},
ID +"="+id,
null, null, null, null);
if(cursor.getCount() > 0){
exist = true;
}
cursor.close();
}catch (SQLException e){
Log.e("DB Error", e.toString());
e.printStackTrace();
}
return exist;
}
/** this code is used to get all data from database */
public boolean isPreviousDataExist(){
boolean exist = false;
Cursor cursor = null;
try{
cursor = db.query(
TABLE_NAME,
new String[]{ID},
null,null, null, null, null);
if(cursor.getCount() > 0){
exist = true;
}
cursor.close();
}catch (SQLException e){
Log.e("DB Error", e.toString());
e.printStackTrace();
}
return exist;
}
public void addData(long id, String menu_name, int quantity, double total_price){
// this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(ID, id);
values.put(MENU_NAME, menu_name);
values.put(QUANTITY, quantity);
values.put(TOTAL_PRICE, total_price);
// ask the database object to insert the new data
try{db.insert(TABLE_NAME, null, values);}
catch(Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void deleteData(long id){
// ask the database manager to delete the row of given id
try {db.delete(TABLE_NAME, ID + "=" + id, null);}
catch (Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void deleteAllData(){
// ask the database manager to delete the row of given id
try {db.delete(TABLE_NAME, null, null);}
catch (Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void updateData(long id, int quantity, double total_price){
// this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(QUANTITY, quantity);
values.put(TOTAL_PRICE, total_price);
// ask the database object to update the database row of given rowID
try {db.update(TABLE_NAME, values, ID + "=" + id, null);}
catch (Exception e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}}
答案 0 :(得分:1)
在db中执行任何事务之前,必须先获取可写数据库 在twik.in.ActivityCart.getDataFromDatabase(ActivityCart.java:378)
在执行数据库操作之前打开数据库并尽快关闭它。 示例
switch(FLAG){
case 0:
// clear all menu in order table
dbhelper.openDataBase();
dbhelper.deleteAllData();
dbhelper.close();
listOrder.invalidateViews();
clearData();
new getDataTask().execute();
break;
case 1:
// clear selected menu in order table
dbhelper.openDataBase();
dbhelper.deleteData(ID);
dbhelper.close();
listOrder.invalidateViews();
clearData();
new getDataTask().execute();
break;
}
注意:您必须在完成事务后立即关闭数据库,否则如果您的数据库保持打开状态这么长时间,您的应用程序可能会抛出数据库对象泄露的异常。