SQLite没有空构造函数

时间:2013-06-28 07:02:31

标签: android sqlite

我开始使用Android了。我想创建一个数据库。但是日志显示错误没有我的服务的空构造函数。我不能添加任何空构造函数。一定有我做错的事。但我找不到它。请帮帮我。

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);

}}

我的其他课程

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>

日志

 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

3 个答案:

答案 0 :(得分:2)

您已更正的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>

希望这应该有用

答案 1 :(得分:0)

您将Ebase声明为XML中的活动,但Ebase派生自SQLiteOpenHelper根本未实现Activity。我想你的意思是将Ebase2放在第一个<activity />声明中(在这种情况下,删除第二个<activity />声明)。

答案 2 :(得分:0)

我认为你应该在你的ebase类中添加以下代码:

public Ebase() {

}