android 上的 SQLite 意外停止
SQLite on android stop unexpectedly
我在使用 sqlite android。 运行项目的时候,遇到了一些麻烦。
我有两件事。 VivsHelper 是前者,MainActivity 是 latter.Here 是我的代码:
classVivzHelper:
public class VivzHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "vivzdatabase";
private static final String TABLE_NAME = "VIVZTABLE";
private static final int DATABASE_VERSION = 1;
private static final String UID = "_id";
private static final String NAME = "_Name";
private Context context;
private static final String CREATE_TABLE = "CREATE TABLE" + TABLE_NAME +" ("+ UID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +NAME+"_Name VARCHAR(255));";
private static final String DROP_TABLE = "DROP TABLE "+ TABLE_NAME + " IF EXISTS";
public VivzHelper(Context context){
super(context, "", null, 1);
this.context = context;
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
db.execSQL(CREATE_TABLE);
Toast.makeText(context, "Oncreate called", Toast.LENGTH_LONG).show();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
try {
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是 MainActivity:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
VivzHelper vivzHelper;
vivzHelper = new VivzHelper(this);
SQLiteDatabase sqLiteDatabase = vivzHelper.getWritableDatabase();
当我 运行 时,我的应用程序突然停止了。这是为什么?帮帮我
这是日志猫:
04-06 23:48:39.736: E/AndroidRuntime(535): FATAL EXCEPTION: main
04-06 23:48:39.736: E/AndroidRuntime(535): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlitefull/com.example.sqlitefull.MainActivity}: java.lang.StringIndexOutOfBoundsException
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread.access00(ActivityThread.java:125)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.os.Looper.loop(Looper.java:123)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-06 23:48:39.736: E/AndroidRuntime(535): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 23:48:39.736: E/AndroidRuntime(535): at java.lang.reflect.Method.invoke(Method.java:521)
04-06 23:48:39.736: E/AndroidRuntime(535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-06 23:48:39.736: E/AndroidRuntime(535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-06 23:48:39.736: E/AndroidRuntime(535): at dalvik.system.NativeStart.main(Native Method)
04-06 23:48:39.736: E/AndroidRuntime(535): Caused by: java.lang.StringIndexOutOfBoundsException
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ContextImpl.validateFilePath(ContextImpl.java:1579)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:539)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
04-06 23:48:39.736: E/AndroidRuntime(535): at com.example.sqlitefull.MainActivity.onCreate(MainActivity.java:23)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-06 23:48:39.736: E/AndroidRuntime(535): ... 11 more
是的,正如@Shifar 所说,super class 的参数无效,您必须传递数据库名称!还要检查你的 Create 语句,你在其中得到了名称“+NAME+”_Name VARCHAR(255));”和一个分号
我在使用 sqlite android。 运行项目的时候,遇到了一些麻烦。 我有两件事。 VivsHelper 是前者,MainActivity 是 latter.Here 是我的代码: classVivzHelper:
public class VivzHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "vivzdatabase";
private static final String TABLE_NAME = "VIVZTABLE";
private static final int DATABASE_VERSION = 1;
private static final String UID = "_id";
private static final String NAME = "_Name";
private Context context;
private static final String CREATE_TABLE = "CREATE TABLE" + TABLE_NAME +" ("+ UID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +NAME+"_Name VARCHAR(255));";
private static final String DROP_TABLE = "DROP TABLE "+ TABLE_NAME + " IF EXISTS";
public VivzHelper(Context context){
super(context, "", null, 1);
this.context = context;
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
db.execSQL(CREATE_TABLE);
Toast.makeText(context, "Oncreate called", Toast.LENGTH_LONG).show();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
try {
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是 MainActivity:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
VivzHelper vivzHelper;
vivzHelper = new VivzHelper(this);
SQLiteDatabase sqLiteDatabase = vivzHelper.getWritableDatabase();
当我 运行 时,我的应用程序突然停止了。这是为什么?帮帮我
这是日志猫:
04-06 23:48:39.736: E/AndroidRuntime(535): FATAL EXCEPTION: main
04-06 23:48:39.736: E/AndroidRuntime(535): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlitefull/com.example.sqlitefull.MainActivity}: java.lang.StringIndexOutOfBoundsException
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread.access00(ActivityThread.java:125)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.os.Looper.loop(Looper.java:123)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-06 23:48:39.736: E/AndroidRuntime(535): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 23:48:39.736: E/AndroidRuntime(535): at java.lang.reflect.Method.invoke(Method.java:521)
04-06 23:48:39.736: E/AndroidRuntime(535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-06 23:48:39.736: E/AndroidRuntime(535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-06 23:48:39.736: E/AndroidRuntime(535): at dalvik.system.NativeStart.main(Native Method)
04-06 23:48:39.736: E/AndroidRuntime(535): Caused by: java.lang.StringIndexOutOfBoundsException
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ContextImpl.validateFilePath(ContextImpl.java:1579)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:539)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
04-06 23:48:39.736: E/AndroidRuntime(535): at com.example.sqlitefull.MainActivity.onCreate(MainActivity.java:23)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-06 23:48:39.736: E/AndroidRuntime(535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-06 23:48:39.736: E/AndroidRuntime(535): ... 11 more
是的,正如@Shifar 所说,super class 的参数无效,您必须传递数据库名称!还要检查你的 Create 语句,你在其中得到了名称“+NAME+”_Name VARCHAR(255));”和一个分号