我在android中使用android studio尝试了一个简单的注册应用程序。我正在尝试创建一个简单的数据库并在其中插入值。我收到以下错误,当我调试我的应用程序时,我无法理解。
FATAL EXCEPTION: main android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
任何建议都将受到高度赞赏。 java代码在这里:
FunTube
get
和set
值
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 ;
}
}
registration.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();
}
}
答案 0 :(得分:3)
很简单,你已经定义了列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();
但由于db
中没有数据,因此无法获取数据现在将值传递给videopath而不是null值,然后尝试
例如。替换这一行
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);
它会起作用
如有更多问题,请告诉我
答案 1 :(得分:2)
// This should work
private ArrayList<FunTube> parseFunTube(Cursor cursor) {
ArrayList<Funtube> listofallmyFunTubes = new ArrayList<Funtube>();
if (cursor != null) {
// move cursor to first row
if (cursor.moveToFirst()) {
do {
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));
listofallmyFunTubes.add(ft);
// move to next row
} while (cursor.moveToNext());
}
return listofallmyFuntubes;
}