Adaichi
Adaichi

Reputation: 45

SQLite no empty constructor

I am getting started to Android. I wanted to create a database. But the log showed the error no empty constructor for my service. I cant add any empty constructor. There must be something I do wrong. But i couldn't find it. Please help me.

MainActivity.java:

public class Ebase extends SQLiteOpenHelper {

private static final String BOOKS = "book.db";
private static final int Version = 1;

public Ebase(Context con) {
    super(con, BOOKS, null, Version);

}

public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE" + "btable"
            + "(bNum TEXT, pName TEXT, price INTEGER, quantity INTEGER )");

}

public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
    db.execSQL("DROP TABLE IF EXIST btable");
    onCreate(db);

}}

My other class

public class Ebase2 extends Activity {

public Ebase book;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ebase);

    book = new Ebase(this);

    final EditText bnumber = (EditText) findViewById(R.id.editText1);
    Button search = (Button) findViewById(R.id.button1);
    search.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            try {
                DataSearch((bnumber.getText()).toString());

            } finally {
                book.close();
            }

        }
    });

}

private static final String[] SELECT = { "bNum" };

protected void DataSearch(String string) {
    SQLiteDatabase db = book.getReadableDatabase();
    Cursor c = db.query("btable", SELECT, null, null, null, null, null);
    while (c.moveToNext()) {
        if (c.toString() == string)
            GettingData(c);

    }

}

private void GettingData(Cursor c) {
    StringBuilder builder = new StringBuilder("Information\n");
    String pName = c.getString(c.getColumnIndex("pName"));
    String price = c.getString(c.getColumnIndex("price"));
    String quantity = c.getString(c.getColumnIndex("quantity"));
    builder.append(pName).append("Product Name:");
    builder.append(price).append("Price:");
    builder.append(quantity).append("Quantity:");

    TextView result = (TextView) findViewById(R.id.textView1);

}}

Activity Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.examplebase"
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.examplebase.Ebase"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:label="@string/app_name"  android:name="Ebase2"/>
</application></manifest>

Log

 E/Trace(1454): error opening trace file: No such file or directory (2)
 D/dalvikvm(1454): newInstance failed: no ()
 D/AndroidRuntime(1454): Shutting down VM
 W/dalvikvm(1454): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
 E/AndroidRuntime(1454): FATAL EXCEPTION: main
 E/AndroidRuntime(1454): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.examplebase/com.example.examplebase.Ebase}: java.lang.InstantiationException: can't instantiate class com.example.examplebase.Ebase; no empty constructor
 E/AndroidRuntime(1454):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
 E/AndroidRuntime(1454):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
 E/AndroidRuntime(1454):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
 E/AndroidRuntime(1454):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
 E/AndroidRuntime(1454):    at android.os.Handler.dispatchMessage(Handler.java:99)
 E/AndroidRuntime(1454):    at android.os.Looper.loop(Looper.java:137)
 E/AndroidRuntime(1454):    at android.app.ActivityThread.main(ActivityThread.java:5041)
 E/AndroidRuntime(1454):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1454):    at java.lang.reflect.Method.invoke(Method.java:511)
 E/AndroidRuntime(1454):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 E/AndroidRuntime(1454):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 E/AndroidRuntime(1454):    at dalvik.system.NativeStart.main(Native Method)
 E/AndroidRuntime(1454): Caused by: java.lang.InstantiationException: can't instantiate class com.example.examplebase.Ebase; no empty constructor
 E/AndroidRuntime(1454):    at java.lang.Class.newInstanceImpl(Native Method)
 E/AndroidRuntime(1454):    at java.lang.Class.newInstance(Class.java:1319)
 E/AndroidRuntime(1454):    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
 E/AndroidRuntime(1454):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
 E/AndroidRuntime(1454):    ... 11 more

Upvotes: 1

Views: 539

Answers (3)

Quoc Truong
Quoc Truong

Reputation: 545

i think you should add follow code to your ebase class:

public Ebase() {

}

Upvotes: 0

Benil Mathew
Benil Mathew

Reputation: 1634

Your Corrected AndroidManifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.examplebase"
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.examplebase.Ebase2"
        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>

Hope this should work

Upvotes: 2

Jean Hominal
Jean Hominal

Reputation: 16796

You are declaring Ebase as an activity in the XML, but Ebase derives from SQLiteOpenHelper that does not implement Activity at all. I guess that you meant to put Ebase2 in the first <activity /> declaration (and in that case, remove the second <activity /> declaration).

Upvotes: 0

Related Questions