Hi I'm trying to make a task with SQLite database in android.
The problem is it run without any error and without any warning, but when i tried to see the content comes from database the program stooped
I don't know why this error occurs. I tried several things but can't find any solution. help me please.
My DbCopy.java
package spurs.cafesoft.fragment;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DbCopy extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/spurs.spurs.cafesoft.fragment/databases/";
private static String DB_NAME = "studenttoolkit.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DbCopy(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext = context;
}
public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}
/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 *
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        // Log.d("myLog", "database does't exist yet.");
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
    InputStream myInput = myContext.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;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
    if (myDataBase != null)
        myDataBase.close();
    super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
My Fragment_versity.java is
package spurs.cafesoft.fragment;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
import spurs.cafesoft.studenttookit.R;
public class Fragment_versity  extends Fragment {
ListView listView;
ArrayList<String> dbList=new ArrayList<String>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.fragment_versity, container, false);
    ListView listView = (ListView) rootview.findViewById(R.id.vrstycontent);
    caliingDpCopy();
    getContent();
    listView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,dbList));
    return rootview;
}
@SuppressLint("SdCardPath") private void getContent() {
    // TODO Auto-generated method stub
    String dbPath = "/data/data/spurs.cafesoft.fragment/databases/studenttoolkit.db";
    SQLiteDatabase myDatabase = null;
    try {
        myDatabase = SQLiteDatabase.openDatabase(dbPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (Exception e) {
    }
    String sql = "SELECT description FROM virsity";
    Cursor c = null;
    try {
        c = myDatabase.rawQuery(sql, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        dbList.add(c.getString(c.getColumnIndex("description")));
    }
    myDatabase.close();
}
private void caliingDpCopy() {
    // TODO Auto-generated method stub
    DbCopy mydb = new DbCopy(getActivity());
    try {
        mydb.createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
and finally my fragment_versity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:background="#7f8c8d"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="100dp">
    <ImageView
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:id="@+id/imageView2"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/diulogo"/>
</RelativeLayout>
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/border_one">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView
            android:id="@+id/vrstycontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </RelativeLayout>
</ScrollView>
</LinearLayout>
my logcat shows this
77/spurs.cafesoft.studenttookit E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: spurs.cafesoft.studenttookit, PID: 4177
                                                                        java.lang.Error: Error copying database
                                                                            at spurs.cafesoft.fragment.DbCopy.createDataBase(DbCopy.java:48)
                                                                            at spurs.cafesoft.fragment.Fragment_versity.caliingDpCopy(Fragment_versity.java:91)
                                                                            at spurs.cafesoft.fragment.Fragment_versity.onCreateView(Fragment_versity.java:53)
                                                                            at android.app.Fragment.performCreateView(Fragment.java:2053)
                                                                            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
                                                                            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                            at android.app.BackStackRecord.run(BackStackRecord.java:834)
                                                                            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
                                                                            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:135)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Aucun commentaire:
Enregistrer un commentaire