gpt4 book ai didi

java - android 中的 CursorIndexOutOfBoundsException

转载 作者:太空宇宙 更新时间:2023-11-03 12:49:40 38 4
gpt4 key购买 nike

我使用 android studio 在 android 中尝试了一个简单的注册应用程序。我正在尝试制作一个简单的数据库并在其中插入值。我在调试我的应用程序时收到以下无法理解的错误。

FATAL EXCEPTION: main android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

任何建议将不胜感激。Java 代码在这里:

趣味视频

获取设置值的类

DatabaseWrapper.java

public class DatabaseWrapper extends SQLiteOpenHelper {
public static final String FUNTUBE= "User";
public static final String FUNTUBE_ID = "_id";
public static final String FUNTUBE_UNAME = "_username";
public static final String FUNTUBE_EMAIL = "_email";
public static final String FUNTUBE_PASSWORD = "_password";
public static final String FUNTUBE_FNAME="_fname";
public static final String FUNTUBE_LNAME = "_lname";
public static final String FUNTUBE_PHONE="_phone";
public static final String FUNTUBE_COUNTRY = "_country";
public static final String FUNTUBE_GENDER = "_gender";
public static final String FUNTUBE_VIDEOPATH= "_videopath";
public static final String FUNTUBE_BDAY = "_bday";
public static final String FUNTUBE_YEAR = "_year";
public static final String FUNTUBE_INTEREST = "_interest";
public static final String FUNTUBE_RELIGION = "_religion";
public static final String FUNTUBE_ABOUT="_about";
public static final String FUNTUBE_QUOTE="_qoute";
public static final String FUNTUBE_JOB = "_job";

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);

}


private static final String DATABASE_NAME = "FunTube.db";
private static final int DATABASE_VERSION = 1;

// creation SQLite statement
private static final String DATABASE_CREATE = "create table " +FUNTUBE
+ "(" + FUNTUBE_ID + " integer primary key autoincrement, "

+ FUNTUBE_UNAME + " text not null,"
+FUNTUBE_EMAIL + " text not null,"
+FUNTUBE_PASSWORD + " text not null,"
+ FUNTUBE_FNAME + " text not null,"
+FUNTUBE_LNAME + " text not null,"
+FUNTUBE_PHONE + " text not null,"
+ FUNTUBE_COUNTRY + " text not null,"
+FUNTUBE_GENDER + " text not null,"
+FUNTUBE_VIDEOPATH + " text not null,"
+ FUNTUBE_BDAY + " text not null,"
+FUNTUBE_YEAR + " text not null,"
+FUNTUBE_INTEREST + " text not null,"
+ FUNTUBE_RELIGION + " text not null,"
+FUNTUBE_ABOUT + " text not null,"
+FUNTUBE_QUOTE + " text not null,"
+FUNTUBE_JOB+ " text not null)";




public DatabaseWrapper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

if(newVersion>oldVersion) {
db.execSQL("DROP TABLE IF EXISTS " + FUNTUBE);
onCreate(db);
}

}

}

FunTubeOperations

public class FunTubeOperations {
private DatabaseWrapper dbHelper;
private String[] FUNTUBE_TABLE_COLUMNS = { DatabaseWrapper.FUNTUBE_ID, DatabaseWrapper.FUNTUBE_UNAME,DatabaseWrapper.FUNTUBE_EMAIL, DatabaseWrapper.FUNTUBE_PASSWORD,DatabaseWrapper.FUNTUBE_FNAME, DatabaseWrapper.FUNTUBE_LNAME,DatabaseWrapper.FUNTUBE_PHONE, DatabaseWrapper.FUNTUBE_COUNTRY,DatabaseWrapper.FUNTUBE_GENDER, DatabaseWrapper.FUNTUBE_VIDEOPATH,DatabaseWrapper.FUNTUBE_BDAY, DatabaseWrapper.FUNTUBE_YEAR,DatabaseWrapper.FUNTUBE_INTEREST, DatabaseWrapper.FUNTUBE_RELIGION,DatabaseWrapper.FUNTUBE_ABOUT, DatabaseWrapper.FUNTUBE_QUOTE, DatabaseWrapper.FUNTUBE_JOB};

private SQLiteDatabase database;

public FunTubeOperations(Context context) {
dbHelper = new DatabaseWrapper(context);
}

public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}

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

public FunTube addUsers(String uname,String email,String password,String lname,String fname,String phone,String country,String gender,String videopath,String bday,String year,String interest,String religion,String about,String qoute,String job) {

ContentValues values = new ContentValues();

values.put(DatabaseWrapper.FUNTUBE_UNAME, uname);
values.put(DatabaseWrapper.FUNTUBE_EMAIL, email);
values.put(DatabaseWrapper.FUNTUBE_PASSWORD, password);
values.put(DatabaseWrapper.FUNTUBE_FNAME, fname);
values.put(DatabaseWrapper.FUNTUBE_LNAME, lname);
values.put(DatabaseWrapper.FUNTUBE_PHONE, phone);
values.put(DatabaseWrapper.FUNTUBE_COUNTRY, country);
values.put(DatabaseWrapper.FUNTUBE_GENDER, gender);
values.put(DatabaseWrapper.FUNTUBE_VIDEOPATH, videopath);
values.put(DatabaseWrapper.FUNTUBE_BDAY, bday);
values.put(DatabaseWrapper.FUNTUBE_YEAR, year);
values.put(DatabaseWrapper.FUNTUBE_INTEREST, interest);
values.put(DatabaseWrapper.FUNTUBE_RELIGION, religion);
values.put(DatabaseWrapper.FUNTUBE_ABOUT, about);
values.put(DatabaseWrapper.FUNTUBE_QUOTE, qoute);
values.put(DatabaseWrapper.FUNTUBE_JOB, job);



long FuntubeId = database.insert(DatabaseWrapper.FUNTUBE, null, values);


Cursor cursor = database.query(DatabaseWrapper.FUNTUBE, FUNTUBE_TABLE_COLUMNS, DatabaseWrapper.FUNTUBE_ID + " = " + FuntubeId, null, null, null, null);

cursor.moveToFirst();


FunTube newComment = parseFunTube(cursor);
cursor.close();
return newComment;
}
private FunTube parseFunTube(Cursor cursor) {
FunTube ft = new FunTube();
ft .setId((cursor.getInt(0)));
ft .setUname(cursor.getString(1));
ft .setEmail(cursor.getString(2));
ft .setPassword(cursor.getString(3));
ft .setFname(cursor.getString(4));
ft .setLname(cursor.getString(5));
ft .setPhone(cursor.getString(6));
ft .setCountry(cursor.getString(7));
ft .setGender(cursor.getString(8));
ft .setVideopath(cursor.getString(9));
ft .setBday(cursor.getString(10));
ft .setYear(cursor.getString(11));
ft.setInterest(cursor.getString(12));
ft.setReligion(cursor.getString(13));
ft .setAbout(cursor.getString(14));
ft .setQuote(cursor.getString(15));
ft .setJob(cursor.getString(16));


return ft ;
}
}

注册.java

public class registeration extends AppCompatActivity {

private FunTubeOperations FunTubeDBOperations;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registeration);
final AlertDialog ad = new AlertDialog.Builder(this).create();
Button b1 = (Button) findViewById(R.id.register);
FunTubeDBOperations = new FunTubeOperations(this);
FunTubeDBOperations.open();

try {
b1.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View arg0) {


EditText username = (EditText) findViewById(R.id.username);
EditText email = (EditText) findViewById(R.id.email);
EditText fname = (EditText) findViewById(R.id.fname);
EditText lname = (EditText) findViewById(R.id.lname);
EditText password = (EditText) findViewById(R.id.password);
EditText country = (EditText) findViewById(R.id.country);
EditText phone = (EditText) findViewById(R.id.phone);
EditText bday = (EditText) findViewById(R.id.bday);
EditText year = (EditText) findViewById(R.id.year);
EditText interest = (EditText) findViewById(R.id.interest);
EditText qoute = (EditText) findViewById(R.id.qoute);
EditText about = (EditText) findViewById(R.id.about);
EditText religion = (EditText) findViewById(R.id.religion);
EditText job = (EditText) findViewById(R.id.job);
RadioGroup rg = (RadioGroup) findViewById(R.id.fm);


String user = username.getText().toString();
String em = email.getText().toString();
String fn = fname.getText().toString();
String ln = lname.getText().toString();
String pass = password.getText().toString();
String co = country.getText().toString();
String ph = phone.getText().toString();
String bd = bday.getText().toString();
String ye = year.getText().toString();
String inter = interest.getText().toString();
String q = qoute.getText().toString();
String rel = religion.getText().toString();
String jo = job.getText().toString();
String ab = about.getText().toString();
String gen = ((RadioButton) findViewById(rg.getCheckedRadioButtonId())).getText().toString();


if (user != null) {
FunTube users = FunTubeDBOperations.addUsers(user, em, pass, ln, fn, ph, co, gen, null, bd, ye, inter, rel, ab, q, jo);
if (true) {
Toast.makeText(getApplicationContext(), (String) users.getUname() + " is added successfully!!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(registeration.this, home.class);
startActivity(intent);
finish();
}
} else {
Toast.makeText(getApplicationContext(), "Add some Data...", Toast.LENGTH_LONG).show();
}
}


});


} catch (Exception e) {
ad.setTitle("Error!");
ad.setMessage(e.toString());
}


}
protected void onResume() {
FunTubeDBOperations.open();
super.onResume();
}

@Override
protected void onPause() {
FunTubeDBOperations.close();
super.onPause();
}
}

最佳答案

很简单,您已经定义了列 FUNTUBE_VIDEOPATH + "text not null," NOT NULL 并且您从这里插入了一个空值

FunTube users = FunTubeDBOperations.addUsers(user, em, pass, ln, fn, ph, co, gen, null, bd, ye, inter, rel, ab, q, jo);

所以你的数据没有插入到数据库并返回-1所以你的表没有任何数据

现在你正在通过这个从数据库取回数据

Cursor cursor = database.query(DatabaseWrapper.FUNTUBE, FUNTUBE_TABLE_COLUMNS, DatabaseWrapper.FUNTUBE_ID + " = " + FuntubeId, null, null, null, null);

cursor.moveToFirst();

但是你得到的不是数据,因为数据库中没有数据

现在将值传递给 videopath 而不是空值,然后尝试

例如。替换这一行

FunTube users = FunTubeDBOperations.addUsers(user, em, pass, ln, fn, ph, co, gen, null, bd, ye, inter, rel, ab, q, jo);

FunTube users = FunTubeDBOperations.addUsers(user, em, pass, ln, fn, ph, co, gen,"some value", bd, ye, inter, rel, ab, q, jo);

它会起作用

如果有更多问题请告诉我

关于java - android 中的 CursorIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34312370/

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