0

我正在做一个安卓应用程序。我试图让数据库的行在文本视图中查看它们。一开始我想取数据库的第一行。然后我使用陀螺仪在屏幕上滑动引号。但我得到这个错误。任何人都可以帮助我吗?非常感谢。

这是幻灯片类

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); 在数据库类

在行:quotedatabase.getQuote(id, s1, s2); 在 ShowQuote 课上

4

2 回答 2

1

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

于 2013-09-29T00:36:16.623 回答
0

错误在这里: s1=cursor.getString(1); 和 s2=cursor.getString(2); 如果字符串长度为 0,则无法获取第二个字符,并且会出现 outofboundexception。

执行此代码也没有效果。在这种情况下,你给你的方法一个字符串。您方法中的此 S1 与您提供给方法的字符串不同。通过为方法提供变量,JAVA 创建了该变量的副本。

于 2013-09-29T00:43:51.480 回答