android - Error: Unfortunately App has stopped -
i trying create create account form through sqlite database when try run on phone or emulator shows "unfortunately app has stopped" .
this mnifest file code
<?xml version="1.0" encoding="utf-8"?>
<application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/theme.appcompat" > <activity android:name=".mainactivity" android:label="@string/app_name" android:theme="@style/apptheme.noactionbar" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application>
this mainactivity
public class mainactivity extends appcompatactivity { databasehelper mydb; edittext editname, editpassword, editemail; button buttonregister; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mydb=new databasehelper(this); editname = (edittext) findviewbyid(r.id.edit_name); editemail = (edittext) findviewbyid(r.id.edit_email); editpassword = (edittext) findviewbyid(r.id.edit_password); buttonregister = (button) findviewbyid(r.id.buttonregister); reg(); } public void reg(){ buttonregister.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { boolean isinserted=mydb.insertdata(editname.gettext().tostring(), editemail.gettext().tostring(), editpassword.gettext().tostring()); if(isinserted==true) toast.maketext(mainactivity.this, "data inserted", toast.length_short).show(); else toast.maketext(mainactivity.this,"data not inserted",toast.length_short).show(); } } ); }
}
and databasehelper class
public class databasehelper extends sqliteopenhelper { private static final int database_version=1; private static final string database_name="user.db"; private static final string table_name="user"; private static final string col_1="id"; private static final string col_2="name"; private static final string col_3="email"; private static final string col_4="pass"; sqlitedatabase db; public databasehelper(context context) { super(context,database_name,null,database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table" + table_name + "(id integer primary key autoincrement, name text, email text, pass text)"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exist"+table_name); oncreate(db); } public boolean insertdata(string name, string email, string pass) { sqlitedatabase db=this.getwritabledatabase(); contentvalues values=new contentvalues(); contentvalues contentvalues=new contentvalues(); contentvalues.put(col_2,name); contentvalues.put(col_3,email); contentvalues.put(col_4,pass); long result=db.insert(table_name, null, contentvalues); if (result==-1) return false; else return true; }
}
this xml file
<?xml version="1.0" encoding="utf-8"?>
<edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputtype="textpersonname" android:text="name" android:ems="10" android:id="@+id/edit_name" android:layout_alignparenttop="true" android:layout_alignparentstart="true" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textemailaddress" android:ems="10" android:id="@+id/edit_password" android:hint="password" android:layout_below="@+id/edit_email" android:layout_alignparentstart="true" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="textemailaddress" android:ems="10" android:id="@+id/edit_email" android:layout_below="@+id/edit_name" android:layout_alignparentstart="true" android:hint="email" /> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="register" android:id="@+id/buttonregister" android:layout_centervertical="true" android:layout_centerhorizontal="true" />
this logcat
02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: fatal exception: main 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: process: com.example.mubbasher.howdy, pid: 6442 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: android.database.sqlite.sqliteexception: near "tableuser": syntax error (code 1): , while compiling: create tableuser(id integer primary key autoincrement, name text, email text, pass text) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:1113) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:690) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:58) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.database.sqlite.sqlitestatement.<init>(sqlitestatement.java:31) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.database.sqlite.sqlitedatabase.executesql(sqlitedatabase.java:1806) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.database.sqlite.sqlitedatabase.execsql(sqlitedatabase.java:1737) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ com.example.mubbasher.howdy.databasehelper.oncreate(databasehelper.java:32) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.database.sqlite.sqliteopenhelper.getdatabaselocked(sqliteopenhelper.java:252) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:164) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ com.example.mubbasher.howdy.databasehelper.insertdata(databasehelper.java:43) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ com.example.mubbasher.howdy.mainactivity$1.onclick(mainactivity.java:35) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.view.view.performclick(view.java:4661) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.view.view$performclick.run(view.java:19498) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.os.handler.handlecallback(handler.java:733) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:95) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.os.looper.loop(looper.java:146) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ android.app.activitythread.main(activitythread.java:5641) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ java.lang.reflect.method.invokenative(native method) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ java.lang.reflect.method.invoke(method.java:515) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1288) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1104) 02-08 18:37:32.635 6442-6442/com.example.mubbasher.howdy e/androidruntime: @ dalvik.system.nativestart.main(native method) 02-08 18:42:32.685 6442-6442/com.example.mubbasher.howdy i/process: sending signal. pid: 6442 sig: 9
change (add space )
public void oncreate(sqlitedatabase db) { db.execsql("create table " + table_name + " (id integer primary key autoincrement, name text, email text, pass text)"); }
also dont forget fix , ( add space after exist)
public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exist "+table_name); oncreate(db); }
Comments
Post a Comment