lundi 26 janvier 2015

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase

I designed an app and on click in image view and show image in new activity if back app force close. I add list view code and database code if need new activity code say. list view code:



import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class sharedlist extends ListActivity{

private String[] items;
private database db;

private Typeface nazanin;
private Typeface homa;

private SharedPreferences sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sharedlist);

db=new database(this);
db.open();

items=new String[db.count("content")];

nazanin=Typeface.createFromAsset(getAssets(), "Font/nazanin.ttf");
homa=Typeface.createFromAsset(getAssets(), "Font/homa.ttf");
sp=getApplicationContext().getSharedPreferences("setting", 0);

setListAdapter(new AAD());



}









class AAD extends ArrayAdapter{

public AAD() {
super(sharedlist.this,R.layout.row,items);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {


LayoutInflater in=getLayoutInflater();
View row=in.inflate(R.layout.row,parent,false);
final String li=db.Display_shared(position,0);
TextView username=(TextView) row.findViewById(R.id.row_username);
TextView maintext=(TextView) row.findViewById(R.id.row_maintext);

username.setText(db.Display_shared(position, 1).toString());
maintext.setText(db.Display_shared(position, 2).toString());
ImageView image=(ImageView) row.findViewById(R.id.row_image);
image.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//new imageview("link",li).execute();

Intent ed=new Intent(sharedlist.this,showImage.class);
//new imageview("link",li).execute();
ed.putExtra("postid",li);
startActivity(ed);
}
});

if(sp.getString("font", "homa").equals("nazanin")){


maintext.setTypeface(nazanin);


}else if(sp.getString("font", "homa").equals("homa")){

maintext.setTypeface(homa);

}


if(sp.getString("size", "k").equals("k")){

maintext.setTextSize(18);

}else if(sp.getString("size", "k").equals("b")){

maintext.setTextSize(25);

}






return (row);


}






}


@Override
protected void onPause() {
super.onPause();
db.close();
}
}


and database :



import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;



public class database extends SQLiteOpenHelper {


public final String path="data/data/packagename/databases/";
public final String Name="database";
public SQLiteDatabase mydb;

private final Context mycontext;

public database(Context context) {
super(context, "database", null, 1);
mycontext=context;

}

@Override
public void onCreate(SQLiteDatabase arg0) {

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

}


public void useable(){

boolean checkdb=checkdb();

if(checkdb){

}else{

this.getReadableDatabase();

try{
copydatabase();
}catch(IOException e){


}

}



}

public void open(){

mydb=SQLiteDatabase.openDatabase(path+Name, null, SQLiteDatabase.OPEN_READWRITE);

}

public void close(){
mydb.close();
}

public boolean checkdb(){

SQLiteDatabase db=null;
try{
db=SQLiteDatabase.openDatabase(path+Name, null, SQLiteDatabase.OPEN_READONLY);
}
catch(SQLException e)
{

}
return db !=null ? true:false ;

}

public void copydatabase() throws IOException{
OutputStream myOutput = new FileOutputStream(path+Name);
byte[] buffer = new byte[1024];
int length;
InputStream myInput = mycontext.getAssets().open(Name);
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}



public String Display(int row,int fild){
Cursor cu= mydb.query("content", null, null, null, null, null, null);
cu.moveToPosition(row);
String name=cu.getString(fild);
return name;
}

public Integer count(String table){
Cursor cu= mydb.query(table, null, null, null, null, null, null);
int s=cu.getCount();
return s;
}

public void insert(String id,String user,String matn){

ContentValues cv=new ContentValues();
cv.put("ID", id);
cv.put("username", user);
cv.put("matn", matn);
mydb.insert("content", "ID", cv);


}


public String Display_shared(int row,int fild){
Cursor cu= mydb.rawQuery("select * from content order by ID DESC",null);
cu.moveToPosition(row);
String name=cu.getString(fild);
return name;
}


}


log chat :



java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: data/data/package name/databases/database


http://ift.tt/1Jxuq2t


Aucun commentaire:

Enregistrer un commentaire