gpt4 book ai didi

java - Android while !c.isAfterLast() loop not being entered

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:19 25 4
gpt4 key购买 nike

问题 按照教程,每次按下按钮时都尝试将一些简单的字符串放入数据库中。我相信“用户名”被输入到数据库中,而调试它并没有做任何有趣的事情。我的问题是当我想调用 databaseToString() 时,我将 Cursor 设置为第一个条目。但随后它甚至没有输入 while(!c.isAfterLast())。到底是怎么回事?我希望它 dbString 几乎等于明文形式的数据库,但这并没有发生。谢谢

尝试

  • 检查 SO/Google/API 指南/thenewboston 源代码
  • 尝试查看我的 SQLite 数据库,但我不知道该怎么做,所以我做不到
  • 如果您注释掉 databaseToString,那么我猜它会运行,但它只是停在那里。
  • 当我设置 Text.setText(dbString); 时它也会崩溃,但这完全不相关

LogCat 它不会崩溃,如果需要我会发布。

SearchActivity.Java 单击按钮时调用插入

public class SearchActivity extends ActionBarActivity {

EditText Input;
TextView Text;
Database dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);

Input = (EditText) findViewById(R.id.Input);
dbHandler = new Database(this, null, null, 1);
// printDatabase();

}

// Add a user to database
public void insert(View view) {
Users user = new Users(Input.getText().toString());
dbHandler.addUser(user);
printDatabase();
}


public void printDatabase() {
// dbString is returning null
String dbString = dbHandler.databaseToString();
//Technically it starts to crash right at this line
Text.setText(dbString);
Input.setText("");
}
}

用户.java

public class Users {
private int _id;
private String _username;

public Users() {

}

public Users(String username){
this._username = username;
}

public String get_username() {
return _username;
}

public void set_username(String username) {
this._username = username;
}

public int get_id() {
return _id;
}

public void set_id(int id) {
this._id = id;
}
}

Database.java 又名处理程序。查看 databaseToString() 并且由于某种原因没有进入 while 循环。

public class Database extends SQLiteOpenHelper {
// Version number to upgrade database version
// each time if you Add, Edit table, you need to change
// the version number
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "users.db";
private static final String TABLE_USERS = "users";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_USERNAME = "username";


public Database(Context context, String DATABASE_NAME, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
// There must be a ',' in between every column definition
String query = "CREATE TABLE " + TABLE_USERS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_USERNAME + " TEXT " +
");";
db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_USERS);
onCreate(db);
}

//Add a new row to the db
public void addUser(Users user){
ContentValues values = new ContentValues();
values.put(COLUMN_USERNAME, user.get_username());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_USERS, null, values);
db.close();
}

public String databaseToString() {
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_USERS + " WHERE 1";

// Cursor point to a location in your results
Cursor c = db.rawQuery(query, null);
// Move to the first row in your results
c.moveToFirst();

while(!c.isAfterLast()) {
if(c.getString(c.getColumnIndex("username")) != null) {
dbString += c.getString(c.getColumnIndex("username"));
dbString += "\n";
}
}
db.close();
return dbString;
}
}

最佳答案

修复 NullPointerException:

首先,要修复 NullPointerException,请在 onCreate() 中初始化 Text:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);

Input = (EditText) findViewById(R.id.Input);
Text = (TextView) findViewById(R.id.Text); //added
dbHandler = new Database(this, null, null, 1);
// printDatabase();

}

调试技巧:

为了确保插入数据库成功,捕获insert()的返回值:

long retVal = db.insert(TABLE_USERS, null, values);

然后,为了验证 Cursor 中是否存在数据,获取 Cursor 的计数:

Cursor c = db.rawQuery(query, null);
int count = c.getCount(); //added

解决方案:

最后,这里要补充几点。首先,您需要推进 Cursor 以避免无限循环。此外,您应该检查 moveToFirst() 的返回值,如果返回 false,则什么也不做。最后,您应该在使用完 Cursor 后将其关闭,否则会发生内存泄漏。

public String databaseToString() {
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_USERS + " WHERE 1";

// Cursor point to a location in your results
Cursor c = db.rawQuery(query, null);
// Move to the first row in your results
//check that moveToFirst returns true
if (c.moveToFirst()){

while(!c.isAfterLast()) {
if(c.getString(c.getColumnIndex("username")) != null) {
dbString += c.getString(c.getColumnIndex("username"));
dbString += "\n";
}
c.moveToNext(); //added
}

}
c.close(); //added
db.close();
return dbString;
}

关于java - Android while !c.isAfterLast() loop not being entered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30768067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com