앱을 디자인하고 이미지 뷰에서 클릭하여 앱이 강제 종료되면 새 작업으로 이미지를 표시합니다. 새로운 활동 코드가 필요하면 목록보기 코드와 데이터베이스 코드를 추가합니다. 목록보기 코드 :java.lang.IllegalStateException : 이미 닫힌 개체를 다시 열려고 시도합니다. SQLiteDatabase
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();
}
}
및 데이터베이스 :
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;
}
}
로그 채팅 : 여기
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: data/data/package name/databases/database
http://i.stack.imgur.com/iQOUp.png
데이터베이스 조작 후 – KOTIOS
오류 로그를 표시 한 후 적절한 닫기 메소드가 호출되면 약화. logcat maybe –
'onPause()'메소드의'db.close();'행의 주석 처리를 제거하십시오. 왜냐하면 매번 당신의 활동이 그 상태로 들어갈 때마다 데이터베이스가 닫히고 활동이 시작될 때마다 이미 열렸 기 때문에 데이터베이스를 닫을 수 있기 때문입니다. – Piyush