android - App stops on button click with error message "Appname has stopped unfortunately" -


header 1

this code have used. want add name database. when enter name edittext, accepts , when click submit button, shows message "app_name has stopped unfortunately" activity_main.xml

    <textview         android:id="@+id/textview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_alignparenttop="true"         android:layout_margintop="70dp"         android:text="username"         android:textappearance="?android:attr/textappearancelarge" />      <edittext         android:id="@+id/edittext1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignbottom="@+id/textview1"         android:layout_marginleft="28dp"         android:layout_torightof="@+id/textview1"         android:ems="10"         android:inputtype="textpersonname" >          <requestfocus />     </edittext>      <button         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignleft="@+id/edittext1"         android:layout_below="@+id/edittext1"         android:layout_margintop="50dp"         android:text="submit" />  </relativelayout>` 

header 1

main_activity.java

package com.example.dbconcept;  import android.os.bundle; import android.app.activity; import android.database.sqlite.sqlitedatabase;  import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext;  public class mainactivity extends activity implements onclicklistener { button b; string user; edittext usr; sqlitedatabase db;      protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         b=(button)findviewbyid(r.id.button1);         try{             db=openorcreatedatabase("mydb",mode_private,null);             db.execsql("create table table1(user varchar(50))");         }         catch(exception e)         {         }         b.setonclicklistener(this);     }       public void onclick(view arg0) {         user=usr.gettext().tostring();         db.execsql("insert table1 values('"+user+"')");       }    } 

you forget intialized usr edittext.

usr = (edittext) findviewbyid(r.id.edittext1) 

add in oncreate(...) after setcontentview(r.layout.activity_main);


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -