0

I get an error when I run my emulator, and try to start reading or viewing the database..

Please see my logcat at the bottum of this page!

I have a Database activity, a SQLView activity, and an SQLite activity.

The database activity is to run the database, the SQLView activity to view the database, and the last one, my SQLite activity is to wrie to my database.

I use SQLiteAssetHelper, and my .rar file is in my assets/databases/Voedsel.rar .

This are my activities:

My database activity:

package com.jacob.eindproject;

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.database.sqlite.SQLiteDatabase.CursorFactory;

import java.sql.*;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class Database extends SQLiteAssetHelper {

    public static final String KEY_PRODUCT = "Product";
    public static final String KEY_EENHEID = "Eenheid";
    public static final String KEY_KCAL = "Kcal";

    private static final String DATABASE_NAME = "Voedsel";
    private static final String DATABASE_TABLE = "Voeding";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private Context Context;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteAssetHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

    }

        @Override
        public void onUpgrade(SQLiteDatabase Voedsel, int oldVersion, int newVersion) {
            Voedsel.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(Voedsel);
        }



    public Database(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public Database open() throws SQLException{
        ourHelper = new DbHelper(Context);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;        
    }

    public void close() {

    ourHelper.close();
}



    public long createEntry(String product, String kcal, String eenheid) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_PRODUCT, product);
        cv.put(KEY_EENHEID, eenheid);
        cv.put(KEY_KCAL, kcal);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public String getData() {
        // TODO Auto-generated method stub
        String[] columns = new String[]{ KEY_PRODUCT, KEY_EENHEID, KEY_KCAL};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        int iProduct = c.getColumnIndex(KEY_PRODUCT);
        int iEenheid = c.getColumnIndex(KEY_EENHEID);
        int iKcal = c.getColumnIndex(KEY_KCAL);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + c.getString(iProduct) + " " + c.getString(iEenheid) + " " + c.getString(iKcal) + "\n";


        }

        return result;
    }

    public void close(Database database) {
        // TODO Auto-generated method stub

    }



}

My SQLite activity:

public class SQLite extends Activity implements View.OnClickListener {

    Button sqlUpdate, sqlView;
    EditText sqlVoeding, sqlKcal, sqlEenheid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqllite);
        sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
        sqlVoeding = (EditText) findViewById(R.id.etSQLVoeding);
        sqlEenheid = (EditText) findViewById(R.id.etSQLEenheid);
        sqlKcal = (EditText) findViewById(R.id.etSQLKcal);

        sqlView = (Button) findViewById(R.id.bSQLopenView);
        sqlView.setOnClickListener((android.view.View.OnClickListener) this);
        sqlUpdate.setOnClickListener((android.view.View.OnClickListener) this);
    }

    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.bSQLUpdate:


            boolean didItWork = true;
            try{            
            String voeding = sqlVoeding.getText().toString();
            String Kcal = sqlKcal.getText().toString();
            String eenheid = sqlEenheid.getText().toString();

            Database entry = new Database(SQLite.this);
            entry.open();
            entry.createEntry(voeding, Kcal, eenheid);
            entry.close();

            }catch (Exception e ){
                didItWork = false;

            }finally{
                if (didItWork){
                    Dialog d = new Dialog(this);
                    d.setTitle("Heak Yeay");
                    TextView tv = new TextView(this);
                    tv.setText("Succes");
                    d.setContentView(tv);
                    d.show();
            }
        }

            break;
        case R.id.bSQLopenView:
            Intent i = new Intent(this, SQLView.class);
            startActivity(i);


        }
        }

    public void onClick(DialogInterface arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    }

And the SQLView activity:

public class SQLView extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqlview);
        TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
        Database info = new Database(this);
        info.open();
        String data = info.getData();
        info.close();
        tv.setText(data);
    }
}

This is my logcat error when I click the SQLView activity:

    12-16 14:28:26.883: E/AndroidRuntime(1170): FATAL EXCEPTION: main
    12-16 14:28:26.883: E/AndroidRuntime(1170): java.lang.NoClassDefFoundError: com.jacob.eindproject.Database
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at com.jacob.eindproject.SQLView.onCreate(SQLView.java:14)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at android.app.Activity.performCreate(Activity.java:5133)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at android.os.Handler.dispatchMessage(Handler.java:99)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at android.os.Looper.loop(Looper.java:137)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at android.app.ActivityThread.main(ActivityThread.java:5103)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at java.lang.reflect.Method.invoke(Method.java:525)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    12-16 14:28:26.883: E/AndroidRuntime(1170):     at dalvik.system.NativeStart.main(Native Method)

And this is my error when I click my 'write' activity:

12-16 14:27:35.913: E/AndroidRuntime(999): FATAL EXCEPTION: main
12-16 14:27:35.913: E/AndroidRuntime(999): java.lang.NoClassDefFoundError: com.jacob.eindproject.Database
12-16 14:27:35.913: E/AndroidRuntime(999):  at com.jacob.eindproject.SQLite.onClick(SQLite.java:45)
12-16 14:27:35.913: E/AndroidRuntime(999):  at android.view.View.performClick(View.java:4240)
12-16 14:27:35.913: E/AndroidRuntime(999):  at android.view.View$PerformClick.run(View.java:17721)
12-16 14:27:35.913: E/AndroidRuntime(999):  at android.os.Handler.handleCallback(Handler.java:730)
12-16 14:27:35.913: E/AndroidRuntime(999):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 14:27:35.913: E/AndroidRuntime(999):  at android.os.Looper.loop(Looper.java:137)
12-16 14:27:35.913: E/AndroidRuntime(999):  at android.app.ActivityThread.main(ActivityThread.java:5103)
12-16 14:27:35.913: E/AndroidRuntime(999):  at java.lang.reflect.Method.invokeNative(Native Method)
12-16 14:27:35.913: E/AndroidRuntime(999):  at java.lang.reflect.Method.invoke(Method.java:525)
12-16 14:27:35.913: E/AndroidRuntime(999):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-16 14:27:35.913: E/AndroidRuntime(999):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-16 14:27:35.913: E/AndroidRuntime(999):  at dalvik.system.NativeStart.main(Native Method)

Hopefully someone can help me fix my error. Thank you all in advance for taking your time.

Jacob.

4
  • Have you tried a clean build? Project -> clean Commented Dec 16, 2013 at 19:36
  • where is Database class? Commented Dec 16, 2013 at 19:37
  • @AndrewFielden Yes, I have. Commented Dec 16, 2013 at 19:57
  • @Raghunandan The uppest code is the Database activity. That activity contains the Database class. Commented Dec 16, 2013 at 19:58

1 Answer 1

1

I don't think you've added the SQLiteAssetHelper library to your project correctly. You must ensure that the build path for your project is correct, and all referenced libraries are selected.

Right click on your project and select 'Properties'

Click on 'Android'

Make sure your SQLiteAssetHelper library is selected

Sign up to request clarification or add additional context in comments.

1 Comment

I don't see any options to click?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.