I am totally new to database in android,and have tried to make a simple application using sqlite in android.But i have some problem with my code or i cant find whats the problem ,that when i run my project the applicatioon stops unexpectly and also the databse is not created..Please help me..My code is as below:
MainActivity.java
package com.example.dbdemo1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
Button ins,del,upd;
ListView lv;
DbHelper dh;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ins = (Button)findViewById(R.id.button1);
upd = (Button)findViewById(R.id.button2);
del = (Button)findViewById(R.id.button3);
lv = (ListView)findViewById(R.id.listView1);
setUpList();
dh = new DbHelper(this);
Cursor c =getAllData();
registerForContextMenu(lv);
ins.setOnClickListener((OnClickListener) this);
upd.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(ins))
{
startActivity(new Intent(MainActivity.this, insertActivity.class));
}
else
{
//startActivity(new Intent(MainActivity.this, updateActivity.class));
}
}
private Cursor getAllData() {
// TODO Auto-generated method stub
return null;
}
private void setUpList() {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
DBhelper.java
package com.example.dbdemo1;
import android.R.string;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper{
public static String db_name ="myDB.db";
public static String fname = "first name";
public static String lname = "last name";
public static String contact = "contact";
public DbHelper(Context context) {
super(context, db_name, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try
{
db.execSQL("CREATE TABLE register(_ID INTEGER PRIMARY KEY AUTOINCREMENT,fName TEXT,lNmae TEXT,Contact TEXT);");
}
catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
android.util.Log.v("Constants", "Upgrading database which will destroy all data");
onCreate(db);
}
}
Logcat
01-22 07:54:40.003: W/dalvikvm(32581): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-22 07:54:40.013: E/AndroidRuntime(32581): FATAL EXCEPTION: main
01-22 07:54:40.013: E/AndroidRuntime(32581): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dbdemo1/com.example.dbdemo1.MainActivity}: java.lang.ClassCastException: com.example.dbdemo1.MainActivity
01-22 07:54:40.013: E/AndroidRuntime(32581): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-22 07:54:40.013: E/AndroidRuntime(32581): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-22 07:54:40.013: E/AndroidRuntime(32581): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-22 07:54:40.013: E/AndroidRuntime(32581): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-22 07:54:40.013: E/AndroidRuntime(32581): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 07:54:40.013: E/AndroidRuntime(32581): at android.os.Looper.loop(Looper.java:123)
01-22 07:54:40.013: E/AndroidRuntime(32581): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-22 07:54:40.013: E/AndroidRuntime(32581): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 07:54:40.013: E/AndroidRuntime(32581): at java.lang.reflect.Method.invoke(Method.java:521)
01-22 07:54:40.013: E/AndroidRuntime(32581): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-22 07:54:40.013: E/AndroidRuntime(32581): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-22 07:54:40.013: E/AndroidRuntime(32581): at dalvik.system.NativeStart.main(Native Method)
01-22 07:54:40.013: E/AndroidRuntime(32581): Caused by: java.lang.ClassCastException: com.example.dbdemo1.MainActivity
01-22 07:54:40.013: E/AndroidRuntime(32581): at com.example.dbdemo1.MainActivity.onCreate(MainActivity.java:33)
01-22 07:54:40.013: E/AndroidRuntime(32581): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-22 07:54:40.013: E/AndroidRuntime(32581): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-22 07:54:40.013: E/AndroidRuntime(32581): ... 11 more
Please help me for this…thanking you in advance.!
you need to implement OnClickListener interface to use setOnClickListener on Button.
or
you can create anonymous method for particular button like
without implementing OnClickListner interface.