i am an android beginner....coding a very simple app...which takes in an employee id,name and stores it in a database...and shows the details when "DISPLAY" is clicked.
there is no error when i click the save button..but it shows "UNFORTUNATELY THE APP HAS STOPPED" when i click display..please help...here is my code
package com.example.emp_db;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.database.Cursor;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity {
static SQLiteDatabase db= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Myhelper mdh =new Myhelper(this,"employeedb",null,1) ;
db=mdh.getWritableDatabase();
Button save =(Button)findViewById(R.id.button1);
Button disp=(Button)findViewById(R.id.button2);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
EditText eid_us = (EditText)findViewById(R.id.edittext_id) ;
EditText ename_us =(EditText)findViewById(R.id.editText_name) ;
int eid = 22;
String ename ="madhuri";
ContentValues cv =new ContentValues();
cv.put("eno", eid);
cv.put("ename", ename);
db.insert("employee", null, cv);
}
});
disp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String[] col=new String[] {"eno","ename"};
Cursor c= db.query("employee", col, null, null, null, null, null);
String eno_db;
if(c.getString(1)!="") eno_db=c.getString(1); else
eno_db="it is null";
TextView t1=(TextView)findViewById(R.id.text_display);
t1.setText(eno_db);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
*************************************************************
package com.example.emp_db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class Myhelper extends SQLiteOpenHelper {
public Myhelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String str="create table employee(eno integer , ename text)";
db.execSQL(str);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
******************************************************************************************
*/ THIS IS ACTIVITY_MAIN.XML */
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<textview>
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_centerVertical="false"
android:text="@string/emp_id"
android:textSize="40sp" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/edittext_id"
android:layout_alignTop="@+id/button1"
android:text="@string/str_display" />
<textview>
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/edittext_id"
android:layout_marginLeft="61dp"
android:layout_marginTop="26dp"
android:text="@string/emp_name"
android:textSize="30sp" />
<edittext>
android:id="@+id/editText_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="textPersonName" >
<requestfocus />
</edittext>
<edittext>
android:id="@+id/edittext_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/editText_name"
android:layout_marginTop="26dp"
android:text="@string/str_save" />
<textview>
android:id="@+id/text_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText_name"
android:layout_below="@+id/button1"
android:layout_marginTop="55dp"
android:textSize="40sp" />
</textview></edittext></textview></textview></relativelayout>
*******************************************************************************************