Reputation: 221
Tried a lot searching and the solutions given were to add the activity to manifest and add the class to manifest and some more. Didn't get any solution. so pls help.
Learning sqlite database accessing by following online tutorials. Tried the following and getting a lot of errors in the logcat.
Supporting Class DbHelper
public class DbHelper extends SQLiteOpenHelper {
private static final String Name = "Record.db";
private static final int Version = 1;
public DbHelper(Context context){
super(context,Name,null,Version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table vijay(name text, id integer primary key)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table if exists vijay");
onCreate(db);
}
public void addRecord(String name, int id){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",name);
values.put("id", id);
db.insert("vijay", null, values);
db.close();
}
}
Main Activity class
public class MainActivity extends Activity {
DbHelper dbhelper = new DbHelper(getApplicationContext());
EditText et1;
EditText et2;
String s;
int ss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText)findViewById(R.id.editText1);
et2 = (EditText)findViewById(R.id.editText2);
s = et1.getText().toString();
ss = Integer.parseInt(et2.getText().toString());
}
public void add(View v){
dbhelper.addRecord(s,ss);
et1.setText("");
et2.setText("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.databaseaccess"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.databaseaccess.MainActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat shows many errors
07-29 16:48:56.793: E/AndroidRuntime(870): FATAL EXCEPTION: main
07-29 16:48:56.793: E/AndroidRuntime(870): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.databaseaccess/com.example.databaseaccess.MainActivity}: java.lang.NullPointerException
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.os.Looper.loop(Looper.java:123)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-29 16:48:56.793: E/AndroidRuntime(870): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 16:48:56.793: E/AndroidRuntime(870): at java.lang.reflect.Method.invoke(Method.java:521)
07-29 16:48:56.793: E/AndroidRuntime(870): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-29 16:48:56.793: E/AndroidRuntime(870): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-29 16:48:56.793: E/AndroidRuntime(870): at dalvik.system.NativeStart.main(Native Method)
07-29 16:48:56.793: E/AndroidRuntime(870): Caused by: java.lang.NullPointerException
07-29 16:48:56.793: E/AndroidRuntime(870): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
07-29 16:48:56.793: E/AndroidRuntime(870): at com.example.databaseaccess.MainActivity.<init>(MainActivity.java:11)
07-29 16:48:56.793: E/AndroidRuntime(870): at java.lang.Class.newInstanceImpl(Native Method)
07-29 16:48:56.793: E/AndroidRuntime(870): at java.lang.Class.newInstance(Class.java:1429)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-29 16:48:56.793: E/AndroidRuntime(870): ... 11 more
Upvotes: 0
Views: 102
Reputation: 221
I found another mistake that lead to app crash.
As Shani & Abbas mentioned (Thanks to them), instantiating the class in onCreate method.
Values of the edittext fields were stored to a String and an int variable in the onCreate method itself before they could have some values entered. Got rid of 'em now. Put them in the button' onclick event function.
Thanks to all...
Upvotes: -1
Reputation: 2104
Just make global variable for DBHelper....
Like this:
DbHelper dbhelper ;
and then create a object for it in onCreate() method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper = new DbHelper(MainActivity.this);
}
Upvotes: 0
Reputation: 2905
Move the following line into your OnCreate
Method
DbHelper dbhelper = new DbHelper(getApplicationContext());
Upvotes: 2