Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从数据库获取数据的android代码_Android_Nullpointerexception_Android Cursor - Fatal编程技术网

从数据库获取数据的android代码

从数据库获取数据的android代码,android,nullpointerexception,android-cursor,Android,Nullpointerexception,Android Cursor,这是我从数据库中获取数据的代码。在我看来,它应该可以正常工作,但在第一次生成java.lang.NullPointerException异常时。之后,它运行没有问题。要解决此问题,我需要做哪些修改?您可以尝试以下代码。我认为它会工作,不会出现NullPointerException错误 您可以尝试下面的代码。我认为它会工作,不会出现NullPointerException错误 我是这样使用数据库的。你可以这样试试。 首先创建ContactDbAdapter类文件,如下所示 Cursor c =

这是我从数据库中获取数据的代码。在我看来,它应该可以正常工作,但在第一次生成java.lang.NullPointerException异常时。之后,它运行没有问题。要解决此问题,我需要做哪些修改?

您可以尝试以下代码。我认为它会工作,不会出现NullPointerException错误


您可以尝试下面的代码。我认为它会工作,不会出现NullPointerException错误


我是这样使用数据库的。你可以这样试试。 首先创建ContactDbAdapter类文件,如下所示

Cursor c = db.rawQuery("select * from notificationtable", null);

if (c.getCount() > 0) {
  while (cursor.moveToNext()) {
    result.setclassurl(c.getString(c.getColumnIndex("Id")));
    result.settype(c.getString(c.getColumnIndex("type")));
    result.setschool(c.getString(c.getColumnIndex("school")));
    result.setdescription(c.getString(c.getColumnIndex("description")));
    result.settitle(c.getString(c.getColumnIndex("title")));
    result.setdatePosted(c.getString(c.getColumnIndex("datePosted")));
    results.add(result);
  }

}
然后,您可以在活动java类中以这种方式直接调用

public class ContactDbAdapter {

  public static final String KEY_NAME = "name";
  public static final String KEY_SCORE = "score";
  public static final String KEY_ROWID = "_id";

   private static final String DATABASE_CREATE = "create table contactdetails (_id integer primary key autoincrement,"
        + "name text not null, score integer not null);";

  private static final String DATABASE_NAME = "contacts";
  private static final String DATABASE_TABLE = "contactdetails";
  private static final int DATABASE_VERSION = 3;

  public static final String TAG = "ContactDbAdapter";

private final Context mCtx;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(DATABASE_CREATE);
    }

    @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 contactdetails");
        onCreate(db);
    }

}

public ContactDbAdapter open() throws SQLiteException {
    mDbHelper = new DatabaseHelper(mCtx);       
    try {
        mDb = mDbHelper.getWritableDatabase();
        } catch (SQLiteException ex) {
          mDb = mDbHelper.getReadableDatabase();
        }
    return this;
}

public ContactDbAdapter(Context ctx) {
    this.mCtx = ctx;
}

public void close() {
    mDbHelper.close();
}

public long createContact(String name, int score) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_SCORE, score);       

    return mDb.insert(DATABASE_TABLE, null, initialValues);
}

public int updateEntry(long _rowIndex, String name, int score) {

      String where = KEY_ROWID + "=" + _rowIndex;

      ContentValues initialValues = new ContentValues();
      initialValues.put(KEY_NAME, name);
      initialValues.put(KEY_SCORE, score);       

      // TODO fill in the ContentValue based on the new object
      return mDb.update(DATABASE_TABLE, initialValues, where, null);
    }

public boolean deleteContact(long rowId) {
    Toast.makeText(this.mCtx, "RowID:" + rowId, Toast.LENGTH_LONG).show();
    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}

public Cursor fetchAllContacts() {
    String order = KEY_SCORE + " DESC ";
    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
            KEY_SCORE}, null, null, null, null, order);
   }

}

因此,这将从表中获取所有记录。然后光标将以与您使用的相同的方式使用

我是这样使用数据库的。你可以这样试试。 首先创建ContactDbAdapter类文件,如下所示

Cursor c = db.rawQuery("select * from notificationtable", null);

if (c.getCount() > 0) {
  while (cursor.moveToNext()) {
    result.setclassurl(c.getString(c.getColumnIndex("Id")));
    result.settype(c.getString(c.getColumnIndex("type")));
    result.setschool(c.getString(c.getColumnIndex("school")));
    result.setdescription(c.getString(c.getColumnIndex("description")));
    result.settitle(c.getString(c.getColumnIndex("title")));
    result.setdatePosted(c.getString(c.getColumnIndex("datePosted")));
    results.add(result);
  }

}
然后,您可以在活动java类中以这种方式直接调用

public class ContactDbAdapter {

  public static final String KEY_NAME = "name";
  public static final String KEY_SCORE = "score";
  public static final String KEY_ROWID = "_id";

   private static final String DATABASE_CREATE = "create table contactdetails (_id integer primary key autoincrement,"
        + "name text not null, score integer not null);";

  private static final String DATABASE_NAME = "contacts";
  private static final String DATABASE_TABLE = "contactdetails";
  private static final int DATABASE_VERSION = 3;

  public static final String TAG = "ContactDbAdapter";

private final Context mCtx;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(DATABASE_CREATE);
    }

    @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 contactdetails");
        onCreate(db);
    }

}

public ContactDbAdapter open() throws SQLiteException {
    mDbHelper = new DatabaseHelper(mCtx);       
    try {
        mDb = mDbHelper.getWritableDatabase();
        } catch (SQLiteException ex) {
          mDb = mDbHelper.getReadableDatabase();
        }
    return this;
}

public ContactDbAdapter(Context ctx) {
    this.mCtx = ctx;
}

public void close() {
    mDbHelper.close();
}

public long createContact(String name, int score) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_SCORE, score);       

    return mDb.insert(DATABASE_TABLE, null, initialValues);
}

public int updateEntry(long _rowIndex, String name, int score) {

      String where = KEY_ROWID + "=" + _rowIndex;

      ContentValues initialValues = new ContentValues();
      initialValues.put(KEY_NAME, name);
      initialValues.put(KEY_SCORE, score);       

      // TODO fill in the ContentValue based on the new object
      return mDb.update(DATABASE_TABLE, initialValues, where, null);
    }

public boolean deleteContact(long rowId) {
    Toast.makeText(this.mCtx, "RowID:" + rowId, Toast.LENGTH_LONG).show();
    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}

public Cursor fetchAllContacts() {
    String order = KEY_SCORE + " DESC ";
    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
            KEY_SCORE}, null, null, null, null, order);
   }

}

因此,这将从表中获取所有记录。然后光标将以与您使用的相同的方式使用

您可以发布logcat错误输出吗?java.lang.NullPointerException logcat error您可以发布logcat错误输出吗?当光标c=db.rawQueryselect*from notificationtable时,会生成java.lang.NullPointerException logcat errorexception,null;当光标c=db.rawQueryselect*from notificationtable时生成is ExecutionException,null;正在执行