嗨我正在从我的数据库中读取数据,该数据库是使用sqlite管理器创建的,然后复制到我的应用程序的数据文件夹中,但当我尝试打印getcount时,光标返回零,我的代码中出现了什么错误是我的代码是错误
无法开始活动
ComponentInfo {your.pckage.namespac / your.pckage.namespac.TabarActivity},第二个错误
引起:java.lang.NullPointerException
public class DBHelper {
public static final String KEY_ROWID = "id";
public static final String KEY_NAME = "name";
public static final String SMALL = "small";
public static final String LARGE = "large";
public static final String TYPE1 = "type1";
public static final String TYPE2 = "type2";
public static final String TYPE3 = "type3";
public static final String TEX = "tex";
public static final String ORIGIN = "origin";
public static final String DESCRIPTION = "description";
public static final String BOOKMARK = "bookmark";
public static final String KEY_EMAIL = "email";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "vegetable.db";
private static final String DATABASE_TABLE = "mac";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE ="create table mac (id integer primary key autoincrement not null ,"+
"name VARCHAR, small integer,"+
"large integer, type1 integer,"+
"type2 integer, type3 integer,"+
"tex varchar, origin varchar,"+
"description varchar, bookmark varchar);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBHelper(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//---opens the database---
public DBHelper open() throws SQLException
{
//db = DBHelper.getWritableDatabase();
db=DBHelper.getReadableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---deletes a particular contact---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the contacts---
public Cursor getAllContacts()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
SMALL,LARGE,TYPE1,TYPE2,TYPE3,TEX,ORIGIN,DESCRIPTION,BOOKMARK}, null, null, null, null, null);
}
//---retrieves a particular contact---
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
SMALL,LARGE,TYPE1,TYPE2,TYPE3,
TEXTURE,ORIGIN,DESCRIPTION,BOOKMARK}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
System.out.println(mCursor);
if (mCursor != null) {
System.out.println(1);
//Boolean t=mCursor.moveToFirst();
Boolean t=mCursor.moveToNext();
System.out.println(t);
}
return mCursor;
}
//---updates a contact---
public boolean updateContact(long rowId, String secondview)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, secondview);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
public class MyListActivity extends ActivityGroup {
/** Called when the activity is first created. */
String tag = "Events";
String[] presidents;
String[] ima;
int i=0;
Intent intent;
Integer[] imageIDs= {R.drawable.abbaysmall,R.drawable.edalesmall};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listtype);
try {
String destPath = "/data/data/" + getPackageName() + "/databases/vegetable.db";
File f = new File(destPath);
if (!f.exists()) {
CopyDB( getBaseContext().getAssets().open("vegetable.db"),
new FileOutputStream(destPath));
System.out.println("hi");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
DBHelper db = new DBHelper(this);
//---get a contact---
db.open();
Cursor c = db.getContact(1);
System.out.println(c.getCount());
if (c.moveToFirst()){
}else
{
Toast.makeText(this, "No contact found", Toast.LENGTH_LONG).show();
db.close();
}
list.setAdapter(adapter);
//setListAdapter(adapter);
list.setOnItemClickListener(onListClick);
}
public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}
答案 0 :(得分:0)
这些代码是否打印任何消息?(在你的getContact方法中),mCursor是否为null?
if (mCursor != null) {
System.out.println(1);
//Boolean t=mCursor.moveToFirst();
Boolean t=mCursor.moveToNext();
System.out.println(t);
}
编辑:试试这个?从系统ContentProvider加载图像信息
private final Uri PHOTO_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
private final String[] PHOTO_PJT = new String[] {
MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.SIZE,
MediaStore.Images.Media.DESCRIPTION, MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.MIME_TYPE };
ContentResolver mResolver = mContext.getContentResolver();
Cursor c = mResolver.query(PHOTO_URI, PHOTO_PJT, null, null, TITLESEARC);