android - Can't instantiate class, no empty constructor, SQLiteHelper -



i'm trying android application using sqlite when try launch application on smartphone, appears white screen , crash.

so have looked @ logcat , showed errors:

04-18 01:33:32.186: e/androidruntime(26741): fatal exception: main 04-18 01:33:32.186: e/androidruntime(26741): java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.sqliteexample/database.mysqlitehelper}: java.lang.instantiationexception: can't instantiate class database.mysqlitehelper; no empty constructor 04-18 01:33:32.186: e/androidruntime(26741):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2034) 04-18 01:33:32.186: e/androidruntime(26741):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2135) 04-18 01:33:32.186: e/androidruntime(26741):    @ android.app.activitythread.access$700(activitythread.java:140) 04-18 01:33:32.186: e/androidruntime(26741):    @ android.app.activitythread$h.handlemessage(activitythread.java:1237) 04-18 01:33:32.186: e/androidruntime(26741):    @ android.os.handler.dispatchmessage(handler.java:99) 04-18 01:33:32.186: e/androidruntime(26741):    @ android.os.looper.loop(looper.java:137) 04-18 01:33:32.186: e/androidruntime(26741):    @ android.app.activitythread.main(activitythread.java:4921) 04-18 01:33:32.186: e/androidruntime(26741):    @ java.lang.reflect.method.invokenative(native method) 04-18 01:33:32.186: e/androidruntime(26741):    @ java.lang.reflect.method.invoke(method.java:511) 04-18 01:33:32.186: e/androidruntime(26741):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) 04-18 01:33:32.186: e/androidruntime(26741):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) 04-18 01:33:32.186: e/androidruntime(26741):    @ dalvik.system.nativestart.main(native method) 04-18 01:33:32.186: e/androidruntime(26741): caused by: java.lang.instantiationexception: can't instantiate class database.mysqlitehelper; no empty constructor 04-18 01:33:32.186: e/androidruntime(26741):    @ java.lang.class.newinstanceimpl(native method) 04-18 01:33:32.186: e/androidruntime(26741):    @ java.lang.class.newinstance(class.java:1319) 04-18 01:33:32.186: e/androidruntime(26741):    @ android.app.instrumentation.newactivity(instrumentation.java:1068) 04-18 01:33:32.186: e/androidruntime(26741):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2025) 04-18 01:33:32.186: e/androidruntime(26741):    ... 11 more 

here mysqlitehelper class:

package database;  import android.util.log; import android.annotation.suppresslint; import android.content.context; import android.database.databaseerrorhandler; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper;  @suppresslint("newapi") public class mysqlitehelper extends sqliteopenhelper {          public static final string table_comments = "comments";         public static final string column_id = "_id";         public static final string column_comment = "comment";          private static final string database_name = "commments.db";         private static final int database_version = 1;          // database creation sql statement         private static final string database_create = "create table "                 + table_comments + "(" + column_id                 + " integer primary key autoincrement, " + column_comment                 + " text not null);";          public mysqlitehelper(context context) {             super(context, database_name, null, database_version);         }          @override         public void oncreate(sqlitedatabase database) {             database.execsql(database_create);         }             @override         public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {             log.w(mysqlitehelper.class.getname(),             "upgrading database version " + oldversion + " "                     + newversion + ", destroy old data");             db.execsql("drop table if exists " + table_comments);             oncreate(db);         } } 

here testdatabaseactivity class:

package activity;  import java.util.list; import java.util.random;  import model.comment;  import dao.commentsdatasource;  import android.app.listactivity; import android.os.bundle; import android.view.view; import android.widget.arrayadapter;  import com.example.sqliteexample.r;  public class testdatabaseactivity extends listactivity {     private commentsdatasource datasource;      public testdatabaseactivity(){         super();     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_my_sqlite_helper);          datasource = new commentsdatasource(this);         datasource.open();          list<comment> values = datasource.getallcomments();          // use simplecursoradapter show         // elements in listview         arrayadapter<comment> adapter = new arrayadapter<comment>(this,                 android.r.layout.simple_list_item_1, values);         setlistadapter(adapter);     }      // called via onclick attribute     // of buttons in main.xml     public void onclick(view view) {         @suppresswarnings("unchecked")         arrayadapter<comment> adapter = (arrayadapter<comment>) getlistadapter();         comment comment = null;         switch (view.getid()) {             case r.id.add:                 string[] comments = new string[] { "cool", "very nice", "hate it" };                 int nextint = new random().nextint(3);                 // save new comment database                 comment = datasource.createcomment(comments[nextint]);                 adapter.add(comment);                 break;             case r.id.delete:                 if (getlistadapter().getcount() > 0) {                     comment = (comment) getlistadapter().getitem(0);                     datasource.deletecomment(comment);                     adapter.remove(comment);                 }                 break;         }         adapter.notifydatasetchanged();     }      @override     protected void onresume() {         datasource.open();         super.onresume();     }      @override     protected void onpause() {         datasource.close();         super.onpause();     } } 

commentsdatasource class:

import java.util.arraylist; import java.util.list;  import database.mysqlitehelper;  import model.comment;  import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase;  public class commentsdatasource {      private sqlitedatabase database;     private mysqlitehelper dbhelper;     private string[] allcolumns = { mysqlitehelper.column_id,             mysqlitehelper.column_comment };      public commentsdatasource(context context) {         dbhelper = new mysqlitehelper(context);     }       public void open() throws sqlexception {         database = dbhelper.getwritabledatabase();     }      public void close() {         dbhelper.close();     }      public comment createcomment(string comment) {         contentvalues values = new contentvalues();         values.put(mysqlitehelper.column_comment, comment);         long insertid = database.insert(mysqlitehelper.table_comments, null,                 values);         cursor cursor = database.query(mysqlitehelper.table_comments,                 allcolumns, mysqlitehelper.column_id + " = " + insertid, null,                 null, null, null);         cursor.movetofirst();         comment newcomment = cursortocomment(cursor);         cursor.close();         return newcomment;     }      public void deletecomment(comment comment) {         long id = comment.getid();         system.out.println("comment deleted id: " + id);         database.delete(mysqlitehelper.table_comments, mysqlitehelper.column_id         + " = " + id, null);     }      public list<comment> getallcomments() {         list<comment> comments = new arraylist<comment>();          cursor cursor = database.query(mysqlitehelper.table_comments,         allcolumns, null, null, null, null, null);          cursor.movetofirst();         while (!cursor.isafterlast()) {             comment comment = cursortocomment(cursor);             comments.add(comment);             cursor.movetonext();         }         // make sure close cursor         cursor.close();         return comments;     }      private comment cursortocomment(cursor cursor) {         comment comment = new comment();         comment.setid(cursor.getlong(0));         comment.setcomment(cursor.getstring(1));         return comment;     }  } 

i have found java.lang.instantiationexception: can't instantiate class database.mysqlitehelper; no empty constructor, don't know how fix it..

the error clear - didn't write no-arg constructor, yet code requires one.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -