2013-09-29 32 views
0

我正在做一个android应用程序。我正试图在文本视图中查看数据库的行。在开始时,我想采取数据库的第一行。然后我使用陀螺仪来滑动屏幕上的报价。但是我得到这个错误。任何人都可以帮助我吗?非常感谢。游标索引超出范围例外,Android数据库

这是幻灯片类

package com.example.prova1; 

import java.util.HashMap; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.widget.TextView; 



public class SlideQuote extends Activity implements SensorEventListener 
{ 
//a TextView 
private TextView quote; 
private TextView author; 
//the Sensor Manager 
private SensorManager sManager; 
float x; 
int id; 
int total; 
String s1,s2; 
    Database quotedatabase = new Database(this); 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.slide_quote); 

     //get the TextView from the layout file 
     quote = (TextView) findViewById(R.id.textView3); 
     author = (TextView) findViewById(R.id.textView4); 
id=1; 
     //get a hook to the sensor service 

     sManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    if(sManager.getSensorList(Sensor.TYPE_GYROSCOPE).size()!=0){ 
     Sensor s =sManager.getSensorList(Sensor.TYPE_GYROSCOPE).get(o); 
     sManager.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL); 
     quotedatabase.getQuote(id, s1, s2); 
     quote.setText(s1); 
     author.setText(s2); 
    } 
    } 
    //when this Activity starts 
    @Override 
protected void onResume() 
{ 
    super.onResume(); 
    /*register the sensor listener to listen to the gyroscope sensor, use the 
    callbacks defined in this class, and gather the sensor information as quick 
    as possible*/ 
    sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),SensorManager.SENSOR_DELAY_FASTEST); 
} 

    //When this Activity isn't visible anymore 
@Override 
protected void onStop() 
{ 
    //unregister the sensor listener 
    sManager.unregisterListener(this); 
    super.onStop(); 
} 

@Override 
public void onAccuracyChanged(Sensor arg0, int arg1) 
{ 
    //Do nothing. 
} 

@Override 
public void onSensorChanged(SensorEvent event) 
{ 
    //if sensor is unreliable, return void 
    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) 
    { 
    return; 
    } 
    //else it will output the Roll, Pitch and Yawn values 
    x=event.values[2]; 
    total=quotedatabase.getQuotesCount(); 

    if(x>25){ 
    if(id==total) 
    {id=1; 
    } 
    else{ 

    id++; 
    } 
     quotedatabase.getQuote(id, s1, s2); 
     quote.setText(s1); 
     author.setText(s2); 

    } 
    if(x<-25){ 
    if(id==1) 
    {id=total;} 
    else{ 
    id--;} 

     quotedatabase.getQuote(id, s1, s2); 
     quote.setText(s1); 
     author.setText(s2); 
    } 
    } 
@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    sManager.unregisterListener(this); 
    Intent backIntent = new Intent(getApplication(), Quote.class); 
     finish(); 
     startActivity(backIntent); 

} 


} 

这是数据库:

package com.example.prova1; 

/**This class is for the database that is used to store quotes*/ 

import java.util.ArrayList; 
import java.util.HashMap; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class Database extends SQLiteOpenHelper { 

public Database(Context quoteContext){ 

super(quoteContext, "quoteDB", null, 1); 

} 

@Override 
//Create database 
public void onCreate(SQLiteDatabase database) { 

    String query = "CREATE TABLE quotelist (quoteId INTEGER PRIMARY KEY, textQuote TEXT ,textAuthor TEXT)"; 
    String query1 = "INSERT INTO quotelist VALUES ('1', 'RAI', 'uku')"; 


    database.execSQL(query); 
    database.execSQL(query1); 
} 

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

    String query = "DROP TABLE IF EXISTS quotelist"; 

    database.execSQL(query); 
    onCreate(database); 
    } 

//Insert quotes into database 
public void insertItem(HashMap<String, String> queryValues){ 

    SQLiteDatabase database = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 

    values.put("textQuote", queryValues.get("textQuote")); 
    values.put("textAuthor", queryValues.get("textAuthor")); 

    database.insert("quotelist", null, values); 

    database.close(); 

} 

//Update the quotes into the database 

public int updateItem(HashMap<String, String> queryValues) { 

SQLiteDatabase database = this.getWritableDatabase(); 

ContentValues values = new ContentValues(); 

values.put("textQuote", queryValues.get("textQuote")); 
values.put("textAuthor", queryValues.get("textAuthor")); 

return database.update("quotelist", values, "quoteId" + " = ?", new String[] {queryValues.get("quoteId") }); 

} 

//Delete quotes into the database 

public void deleteItem(String id){ 

SQLiteDatabase database = this.getWritableDatabase(); 

String deleteQuery = "DELETE FROM quotelist WHERE quoteId='" + id + "'"; 

database.execSQL(deleteQuery); 
database.close(); 


} 

public ArrayList<HashMap<String, String>> getAllItems(){ 

ArrayList<HashMap<String, String>> itemArrayList = new ArrayList<HashMap<String, String>>(); 

String selectQuery = "SELECT * FROM quotelist"; 

SQLiteDatabase database = this.getWritableDatabase(); 

Cursor cursor = database.rawQuery(selectQuery, null); 

if(cursor.moveToFirst()){ 

    do{ 

    HashMap<String, String> itemMap = new HashMap<String, String>(); 

    itemMap.put("quoteId", cursor.getString(o)); 
    itemMap.put("textQuote", cursor.getString(1)); 
    itemMap.put("textAuthor", cursor.getString(2)); 


    itemArrayList.add(itemMap); 

    } while(cursor.moveToNext()); 

} 
database.close(); 

return itemArrayList; 

} 

public HashMap<String, String> getItemInfo(String id){ 

HashMap<String, String> itemMap = new HashMap<String, String>(); 

SQLiteDatabase database = this.getReadableDatabase(); 

String selectQuery = "SELECT * FROM quotelist WHERE quoteId='" + id + "'"; 

Cursor cursor = database.rawQuery(selectQuery, null); 

if(cursor.moveToFirst()){ 

    do{ 

    itemMap.put("quoteId", cursor.getString(o)); 
    itemMap.put("textQuote", cursor.getString(1)); 
    itemMap.put("textAuthor", cursor.getString(2)); 

    } while(cursor.moveToNext()); 

} 

return itemMap; 

} 

public void getQuote(int id, String s1,String s2) { 
    SQLiteDatabase db = this.getReadableDatabase(); 

    Cursor cursor = db.query("quotelist", new String[] { "quoteId", 
      "textQuote", "textAuthor" }, "quoteId" + "=?", 
      new String[] { String.valueOf(id) }, null, null, null, null); 
    if (cursor != null) 
     cursor.moveToFirst(); 
s1=cursor.getString(1); 
s2=cursor.getString(2); 


} 
public int getQuotesCount() { 
    String countQuery = "SELECT * FROM quotelist" ; 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(countQuery, null); 
    cursor.close(); 

    // return count 
    return cursor.getCount(); 
} 

该错误是翠鸟行:S1 = cursor.getString(1);在DatabaseClass

并在行:quotedatabase.getQuote(id,s1,s2);在ShowQuote类

回答

1

此错误表示光标中没有数据。你的数据库里有数据吗?另外,您应该检查moveToFirst的结果,因为如果光标为空,则会显示一个错误值。

0

错误在这里:s1 = cursor.getString(1);和 s2 = cursor.getString(2); 如果字符串长度为0,则无法获得第二个字符,您将得到一个outofboundexception。

另外它对执行此代码没有影响。在这种情况下,你给你的方法一个字符串。你的方法中的S1不是你给你方法的字符串。通过给方法赋予一个变量,JAVA创建了这个变量的一个副本。

+0

如果这是真的,他会得到一个CursorOutofBound:请求索引2,大小为2或类似的东西,但我同意它对代码没有任何影响的部分,但应该明确指出它创建一个不是Object的基本类型的副本(字符串表现为基本类型) –