從表中讀取一行時出現錯誤。我的應用程序是這樣的:應用程序在從表中讀取行時崩潰
-
在您選擇要顯示
- 然後另一活動將開始出現在數據庫
每一個進入該表的主要活動
- ,當你點擊項目在列表視圖中,另一個活動將開始在編輯文本中加載選定的行列 - 這是發生錯誤的地方。
public class DVD_activity extends AppCompatActivity { String a; String b; DB_Worker dw; int x; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dvd_activity); NapojAdapter(); pridajListener(); } private void NapojAdapter() { dw = new DB_Worker(this); SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this,R.layout.dvd_list_layout,dw.VratKurzorD(),new String[] { DVD.KEY_ID, DVD.KEY_title,DVD.KEY_autor},new int[] {R.id.id2, R.id.nazov, R.id.autor},0); ListView lv = (ListView)findViewById(R.id.listView2); lv.setAdapter(myAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_dvd_activity, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void button4click(View view) { Intent intent = new Intent (this, Pridaj_DVD.class); String verzia = "1"; intent.putExtra("verzia",verzia); startActivityForResult(intent, 1); } private void pridajListener() { AdapterView.OnItemClickListener mMessageClickedHandler = new AdapterView.OnItemClickListener() {public void onItemClick(AdapterView parent, View v, int position, long id) { TextView lv = (TextView)v.findViewById(R.id.id2); x = Integer.parseInt(lv.getText().toString()); DVD s = dw.getDVD(x); a =s.title; b = s.autor; String verzia = "2"; Intent intent = new Intent(DVD_activity.this,Pridaj_DVD.class); intent.putExtra("verzia", verzia); intent.putExtra("a",a); intent.putExtra("b",b); startActivityForResult(intent, 1); }}; ListView myList= (ListView) findViewById(R.id.listView2); myList.setOnItemClickListener(mMessageClickedHandler); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { if (resultCode == 0) { DVD s = new DVD(); s.title = data.getStringExtra("a"); s.autor = data.getStringExtra("b"); dw.addDVD(s); NapojAdapter(); } if (resultCode == 1) { dw.deleteDVD(x); NapojAdapter(); } if (resultCode == 2) { a = data.getStringExtra("a"); b = data.getStringExtra("b"); dw.updateDVD(Integer.toString(x),a,b); NapojAdapter(); } } } }
這裏是DB_WORKER
類:
public class DB_Worker {
public SQLiteDatabase db;
private DB_Helper dbHelper;
public DB_Worker(Context context) {
dbHelper = new DB_Helper(context);
db = dbHelper.getWritableDatabase();
}
public void addDVD(DVD s) {
ContentValues values = new ContentValues();
values.put(DVD.KEY_title, s.title);
values.put(DVD.KEY_autor, s.autor);
long newRowId = db.insert(DVD.TABLE,null,values);
}
public DVD getDVD(long ID) {
DVD s = new DVD();
VratKurzorD();
String[] projection = {DVD.KEY_title, DVD.KEY_autor};
String selection = DVD.KEY_ID;
String[] selectionArgs = {""+ID};
Cursor c = db.query(DVD.TABLE,projection,selection,selectionArgs,null,null,null);
s.DVD_ID = ID;
s.title = c.getString(c.getColumnIndex(DVD.KEY_title));
s.autor = c.getString(c.getColumnIndex(DVD.KEY_autor));
c.close();
return s;
}
public void updateDVD(String ID, String title, String autor)
{
ContentValues cv = new ContentValues();
cv.put(DVD.KEY_title,title);
cv.put(DVD.KEY_autor, autor);
db.update(DVD.TABLE, cv, DVD.KEY_ID + "= ?", new String[]{ID});
}
public void deleteDVD(int ID) {
db.delete(DVD.TABLE, DVD.KEY_ID + "= ?", new String[]{"" + ID});
}
public Cursor VratKurzorD() {
Cursor c = db.rawQuery("select * from "+DVD.TABLE, null);
c.moveToFirst();
return c;
}
}
,這裏是錯誤:
從日誌12-03 23:11:42.240 2067-2067/com.example.lowrider.pozicovna E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.lowrider.pozicovna, PID: 2067 java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.
at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.example.lowrider.pozicovna.DB_Worker.getDVD(DB_Worker.java:78)
at com.example.lowrider.pozicovna.DVD_activity$1.onItemClick(DVD_activity.java:66)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
at android.widget.AbsListView$3.run(AbsListView.java:3879)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
好,我已經試過了,現在它拋出索姆其他錯誤: 過程:com.example.lowrider.pozicovna,PID:2195 android.database.CursorIndexOutOfBoundsException:指數-1要求,尺寸爲1 在android.database.AbstractCursor.checkPosition(AbstractCursor.java:460) 在android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 在玉米.example.lowrider.pozicovna.DB_Worker.getDVD(DB_Worker.java:81)... – Lowrider
@Lowrider我更新了我的答案。 –
謝謝c.moveToFirst();工作你是我的英雄! – Lowrider