我开始使用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);
}}
活动清单.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):打开跟踪文件时出错:没有这样的文件或目录 (2) D/dalvikvm(1454): newInstance 失败: 否 () D/AndroidRuntime(1454):关闭虚拟机 W/dalvikvm(1454): threadid=1: 线程以未捕获的异常退出 (group=0x40a71930) E/AndroidRuntime(1454):致命异常:主要 E/AndroidRuntime(1454): java.lang.RuntimeException: 无法实例化活动 ComponentInfo{com.example.examplebase/com.example.examplebase.Ebase}: java.lang.InstantiationException: 无法实例化类 com.example.examplebase .Ebase; 没有空的构造函数 E/AndroidRuntime(1454):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106) E/AndroidRuntime(1454):在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) E/AndroidRuntime(1454):在 android.app.ActivityThread.access$600(ActivityThread.java:141) E/AndroidRuntime(1454):在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) E/AndroidRuntime(1454):在 android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(1454):在 android.os.Looper.loop(Looper.java:137) E/AndroidRuntime(1454):在 android.app.ActivityThread.main(ActivityThread.java:5041) E/AndroidRuntime(1454):在 java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(1454):在 java.lang.reflect.Method.invoke(Method.java:511) E/AndroidRuntime(1454):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) E/AndroidRuntime(1454):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) E/AndroidRuntime(1454): 在 dalvik.system.NativeStart.main(Native Method) E/AndroidRuntime(1454):引起:java.lang.InstantiationException:无法实例化类com.example.examplebase.Ebase;没有空的构造函数 E/AndroidRuntime(1454): 在 java.lang.Class.newInstanceImpl(Native Method) E/AndroidRuntime(1454):在 java.lang.Class.newInstance(Class.java:1319) E/AndroidRuntime(1454):在 android.app.Instrumentation.newActivity(Instrumentation.java:1054) E/AndroidRuntime(1454):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) E/AndroidRuntime(1454): ... 11 更多