我正在为数据库编写代码来获取包含三个字段的活动中的数据,
即Truck_id
,employee_id
和password
。
我收到了一些错误;下面是我的代码: 请帮助我,因为我是android新手:
public class DataBaseHelper extends SQLiteOpenHelper {
// default system path of your application database.
// private static String DB_PATH = "/data/data/com.tanzanite.operasoft/databases/";
private static String DB_PATH = "/sdcard/";
private static String DB_NAME = "snowman";
private static SQLiteDatabase myDataBase;
private Context myContext;
private DataBaseHelper dbHelper;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void TestAdapter(Context context)
{
this.myContext= context;
}
public static final String TABLE_NAME = "Login";
private static final int DATABASE_VERSION= 2;
private static final String DATABASE_NAME="snowman";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "employee_id";
public static final String COLUMN_TRUCKID = "truck_id";
public static final int NAME_COLUMN=1;
public static final String KEY_CREATION_DATE="creation_date";
public static final String KEY_TASK="task";
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
private static final String DATABASE_CREATE = "create table "
+ TABLE_NAME + "( " + COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_TRUCKID+ " text not null, "
+ COLUMN_NAME+ " text not null, )";
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
//this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
public DataBaseHelper open() throws SQLException
{
//myDataBase = dbHelper.getWritableDatabase();
return this;
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath );
SQLiteDatabase.openOrCreateDatabase(dbfile, null);
//throw new Error("Database does't exist yet.");
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[128];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
return;
}
public String logindetails()
{
open();
Cursor c=myDataBase.query(TABLE_NAME,
new String[]{COLUMN_NAME,
COLUMN_TRUCKID,
},null, null, null, null, null);
String b="";
c.close();
myDataBase.close();
return b;
}
public void insert(String a, String b){
ContentValues vals = new ContentValues();
vals.put(COLUMN_TRUCKID, "CM-3456");
vals.put(COLUMN_NAME,"Sachin");
//vals.put(KEY_PIN, "12345");
vals.put(COLUMN_TRUCKID, "HR-6788");
vals.put(COLUMN_NAME,"Sameer");
//vals.put(KEY_PIN, "54321");
vals.put(COLUMN_ID, "AM-6123");
vals.put(COLUMN_NAME,"Sahu");
//vals.put(KEY_PIN, "3452");
myDataBase.close();
}
public Cursor fetchdata()
{
open();
Cursor c=myDataBase.query(TABLE_NAME,
new String[]{COLUMN_NAME,
COLUMN_TRUCKID,
},null, null, null, null, null);
c.moveToLast();
myDataBase.close();
return c;
}
public static SQLiteDatabase getDataBase() {
return myDataBase;
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(DATABASE_CREATE);
}catch(Exception e)
{
Log.v( "Press_Data","exception in table created");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
错误是..
06-14 18:22:27.233: D/dalvikvm(25087): GC_CONCURRENT freed 757K, 48% free 3547K/6727K, external 1625K/2137K, paused 4ms+4ms
06-14 18:22:27.393: I/net.osmand(25087): BaseOsmandRender Init render default for 738 ms
06-14 18:22:27.523: D/dalvikvm(25087): GC_EXTERNAL_ALLOC freed 123K, 46% free 3635K/6727K, external 1625K/2137K, paused 62ms
06-14 18:22:27.633: D/AndroidRuntime(25087): Shutting down VM
06-14 18:22:27.633: W/dalvikvm(25087): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 18:22:27.663: E/AndroidRuntime(25087): FATAL EXCEPTION: main
06-14 18:22:27.663: E/AndroidRuntime(25087): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tanzanite.operasoft/com.tanzanite.operasoft.activity.Sw_LoginScreenActivity}: java.lang.NullPointerException
06-14 18:22:27.663: E/AndroidRuntime(25087): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 18:22:27.663: E/AndroidRuntime(25087): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 18:22:27.663: E/AndroidRuntime(25087): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 18:22:27.663: E/AndroidRuntime(25087): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 18:22:27.663: E/AndroidRuntime(25087): at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 18:22:27.663: E/AndroidRuntime(25087): at android.os.Looper.loop(Looper.java:130)
06-14 18:22:27.663: E/AndroidRuntime(25087): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 18:22:27.663: E/AndroidRuntime(25087): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 18:22:27.663: E/AndroidRuntime(25087): at java.lang.reflect.Method.invoke(Method.java:507)
06-14 18:22:27.663: E/AndroidRuntime(25087): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 18:22:27.663: E/AndroidRuntime(25087): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 18:22:27.663: E/AndroidRuntime(25087): at dalvik.system.NativeStart.main(Native Method)
06-14 18:22:27.663: E/AndroidRuntime(25087): Caused by: java.lang.NullPointerException
06-14 18:22:27.663: E/AndroidRuntime(25087): at com.tanzanite.operasoft.database.DataBaseHelper.fetchdata(DataBaseHelper.java:222)
06-14 18:22:27.663: E/AndroidRuntime(25087): at com.tanzanite.operasoft.activity.Sw_LoginScreenActivity.onCreate(Sw_LoginScreenActivity.java:52)
06-14 18:22:27.663: E/AndroidRuntime(25087): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 18:22:27.663: E/AndroidRuntime(25087): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 18:22:27.663: E/AndroidRuntime(25087): ... 11 more
06-14 18:28:37.603: D/dalvikvm(26440): GC_CONCURRENT freed 757K, 48% free 3541K/6727K, external 1625K/2137K, paused 3ms+3ms
06-14 18:28:37.773: I/net.osmand(26440): BaseOsmandRender Init render default for 611 ms
06-14 18:28:37.873: D/dalvikvm(26440): GC_EXTERNAL_ALLOC freed 122K, 46% free 3633K/6727K, external 1625K/2137K, paused 51ms
06-14 18:28:38.013: D/AndroidRuntime(26440): Shutting down VM
06-14 18:28:38.013: W/dalvikvm(26440): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 18:28:38.033: E/AndroidRuntime(26440): FATAL EXCEPTION: main
06-14 18:28:38.033: E/AndroidRuntime(26440): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tanzanite.operasoft/com.tanzanite.operasoft.activity.Sw_LoginScreenActivity}: java.lang.NullPointerException
06-14 18:28:38.033: E/AndroidRuntime(26440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 18:28:38.033: E/AndroidRuntime(26440): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 18:28:38.033: E/AndroidRuntime(26440): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 18:28:38.033: E/AndroidRuntime(26440): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 18:28:38.033: E/AndroidRuntime(26440): at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 18:28:38.033: E/AndroidRuntime(26440): at android.os.Looper.loop(Looper.java:130)
06-14 18:28:38.033: E/AndroidRuntime(26440): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 18:28:38.033: E/AndroidRuntime(26440): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 18:28:38.033: E/AndroidRuntime(26440): at java.lang.reflect.Method.invoke(Method.java:507)
06-14 18:28:38.033: E/AndroidRuntime(26440): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 18:28:38.033: E/AndroidRuntime(26440): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 18:28:38.033: E/AndroidRuntime(26440): at dalvik.system.NativeStart.main(Native Method)
06-14 18:28:38.033: E/AndroidRuntime(26440): Caused by: java.lang.NullPointerException
06-14 18:28:38.033: E/AndroidRuntime(26440): at com.tanzanite.operasoft.database.DataBaseHelper.fetchdata(DataBaseHelper.java:222)
06-14 18:28:38.033: E/AndroidRuntime(26440): at com.tanzanite.operasoft.activity.Sw_LoginScreenActivity.onCreate(Sw_LoginScreenActivity.java:52)
06-14 18:28:38.033: E/AndroidRuntime(26440): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 18:28:38.033: E/AndroidRuntime(26440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 18:28:38.033: E/AndroidRuntime(26440): ... 11 more
06-14 18:40:55.423: D/dalvikvm(29844): GC_CONCURRENT freed 759K, 48% free 3553K/6727K, external 1625K/2137K, paused 5ms+3ms
06-14 18:40:55.633: I/net.osmand(29844): BaseOsmandRender Init render default for 841 ms
06-14 18:40:55.773: D/dalvikvm(29844): GC_EXTERNAL_ALLOC freed 122K, 46% free 3633K/6727K, external 1625K/2137K, paused 58ms
06-14 18:40:55.913: D/AndroidRuntime(29844): Shutting down VM
06-14 18:40:55.913: W/dalvikvm(29844): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 18:40:55.943: E/AndroidRuntime(29844): FATAL EXCEPTION: main
06-14 18:40:55.943: E/AndroidRuntime(29844): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tanzanite.operasoft/com.tanzanite.operasoft.activity.Sw_LoginScreenActivity}: java.lang.NullPointerException
06-14 18:40:55.943: E/AndroidRuntime(29844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 18:40:55.943: E/AndroidRuntime(29844): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 18:40:55.943: E/AndroidRuntime(29844): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 18:40:55.943: E/AndroidRuntime(29844): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 18:40:55.943: E/AndroidRuntime(29844): at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 18:40:55.943: E/AndroidRuntime(29844): at android.os.Looper.loop(Looper.java:130)
06-14 18:40:55.943: E/AndroidRuntime(29844): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 18:40:55.943: E/AndroidRuntime(29844): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 18:40:55.943: E/AndroidRuntime(29844): at java.lang.reflect.Method.invoke(Method.java:507)
06-14 18:40:55.943: E/AndroidRuntime(29844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 18:40:55.943: E/AndroidRuntime(29844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 18:40:55.943: E/AndroidRuntime(29844): at dalvik.system.NativeStart.main(Native Method)
06-14 18:40:55.943: E/AndroidRuntime(29844): Caused by: java.lang.NullPointerException
06-14 18:40:55.943: E/AndroidRuntime(29844): at com.tanzanite.operasoft.database.DataBaseHelper.fetchdata(DataBaseHelper.java:223)
06-14 18:40:55.943: E/AndroidRuntime(29844): at com.tanzanite.operasoft.activity.Sw_LoginScreenActivity.onCreate(Sw_LoginScreenActivity.java:52)
06-14 18:40:55.943: E/AndroidRuntime(29844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 18:40:55.943: E/AndroidRuntime(29844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 18:40:55.943: E/AndroidRuntime(29844): ... 11 more
06-14 19:12:06.293: D/dalvikvm(5930): GC_CONCURRENT freed 763K, 48% free 3549K/6727K, external 1625K/2137K, paused 4ms+5ms
06-14 19:12:06.443: I/net.osmand(5930): BaseOsmandRender Init render default for 689 ms
06-14 19:12:06.553: D/dalvikvm(5930): GC_EXTERNAL_ALLOC freed 120K, 47% free 3632K/6727K, external 1625K/2137K, paused 64ms
06-14 19:12:06.703: D/AndroidRuntime(5930): Shutting down VM
06-14 19:12:06.703: W/dalvikvm(5930): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 19:12:06.723: E/AndroidRuntime(5930): FATAL EXCEPTION: main
06-14 19:12:06.723: E/AndroidRuntime(5930): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tanzanite.operasoft/com.tanzanite.operasoft.activity.Sw_LoginScreenActivity}: java.lang.NullPointerException
06-14 19:12:06.723: E/AndroidRuntime(5930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 19:12:06.723: E/AndroidRuntime(5930): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 19:12:06.723: E/AndroidRuntime(5930): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 19:12:06.723: E/AndroidRuntime(5930): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 19:12:06.723: E/AndroidRuntime(5930): at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 19:12:06.723: E/AndroidRuntime(5930): at android.os.Looper.loop(Looper.java:130)
06-14 19:12:06.723: E/AndroidRuntime(5930): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 19:12:06.723: E/AndroidRuntime(5930): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 19:12:06.723: E/AndroidRuntime(5930): at java.lang.reflect.Method.invoke(Method.java:507)
06-14 19:12:06.723: E/AndroidRuntime(5930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 19:12:06.723: E/AndroidRuntime(5930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 19:12:06.723: E/AndroidRuntime(5930): at dalvik.system.NativeStart.main(Native Method)
06-14 19:12:06.723: E/AndroidRuntime(5930): Caused by: java.lang.NullPointerException
06-14 19:12:06.723: E/AndroidRuntime(5930): at com.tanzanite.operasoft.database.DataBaseHelper.fetchdata(DataBaseHelper.java:224)
06-14 19:12:06.723: E/AndroidRuntime(5930): at com.tanzanite.operasoft.activity.Sw_LoginScreenActivity.onCreate(Sw_LoginScreenActivity.java:49)
06-14 19:12:06.723: E/AndroidRuntime(5930): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 19:12:06.723: E/AndroidRuntime(5930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 19:12:06.723: E/AndroidRuntime(5930): ... 11 more
答案 0 :(得分:1)
从你的日志我看到
Caused by: java.lang.NullPointerException
06-14 19:12:06.723: E/AndroidRuntime(5930): at com.tanzanite.operasoft.database.DataBaseHelper.fetchdata(DataBaseHelper.java:224)
06-14 19:12:06.723: E/AndroidRuntime(5930): at com.tanzanite.operasoft.activity.Sw_LoginScreenActivity.onCreate(Sw_LoginScreenActivity.java:49)
所以,你需要使用debuger并在
上设置breackpointsSw_LoginScreenActivity.java - 第49行(我认为你试图获取数据)
DataBaseHelper.java - 在224上 - 你试图从myDataBase中获取数据
public Cursor fetchdata()
{
open();
Cursor c=myDataBase.query(TABLE_NAME,
new String[]{COLUMN_NAME,
COLUMN_TRUCKID,
},null, null, null, null, null);
c.moveToLast();
myDataBase.close();
return c;
}
但是,在open()方法中
public DataBaseHelper open() throws SQLException
{
//myDataBase = dbHelper.getWritableDatabase();
return this;
}
您对创建数据库实例的注释行 - 所以下一行“Cursor c = myDataBase.query(TABLE_NAME,”它不正确,因为myDataBase为null。
您没有打开与数据库的连接。
答案 1 :(得分:1)
你问题在于这里(一个空指针异常)根据logcat:
public String logindetails()
{
open();
Cursor c=myDataBase.query(TABLE_NAME, new String[]{COLUMN_NAME, COLUMN_TRUCKID, },
null, null, null, null, null);
String b="";
c.close();
myDataBase.close();
return b;
}
我看到几个问题......
首先,您不应该尝试在db帮助程序的方法中打开/关闭数据库。 db应该由另一个实例化dbhelper的类打开和关闭,并使用实例化的类方法来打开和关闭db。
其次,表列数组列表中有一个额外的逗号。
第三,你似乎无缘无故地调用光标......你得到它并且不对它做任何事情,然后关闭它。
第四,你设置一个空字符串,什么也不做,然后返回它。
我认为看起来应该更像这样:
在您的通话类中:
private DatabaseHelper mDbHelper;
mDbHelper = new DatabaseHelper(this); // instantiate the helper
mDbHelper.openDataBase(); // open the db
String ldetails = mDbHelper.logindetails(); // get logindetails string from db
在您的DatabaseHelper类中:
public String logindetails()
{
String b="";
Cursor c=myDataBase.query(TABLE_NAME, new String[]{COLUMN_NAME, COLUMN_TRUCKID, },
null, null, null, null, null);
c.moveToFirst();
// Insert code to get data from cursor to string
c.close();
return b;
}