Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
光标索引越界异常,Android数据库_Android_Database_Gyroscope - Fatal编程技术网

光标索引越界异常,Android数据库

光标索引越界异常,Android数据库,android,database,gyroscope,Android,Database,Gyroscope,我正在做一个android应用程序。我试图获取数据库中的行,并在文本视图中查看它们。首先,我想获取数据库的第一行。然后我使用陀螺仪在屏幕上滑动引号。但我得到了这个错误。有人能帮我吗?非常感谢 这是幻灯片课 package com.example.prova1; import java.util.HashMap; import android.app.Activity; import android.content.Context; import android.content.Intent;

我正在做一个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;
导入java.util.HashMap;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.hardware.Sensor;
导入android.hardware.SensorEvent;
导入android.hardware.SensorEventListener;
导入android.hardware.SensorManager;
导入android.os.Bundle;
导入android.widget.TextView;
公共类slidekote扩展活动实现SensorEventListener
{
//文本视图
私有文本视图引用;
私有文本视图作者;
//传感器管理器
私人经理;
浮动x;
int-id;
整数合计;
字符串s1、s2;
Database quotedatabase=新数据库(此数据库);
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(右布局幻灯片);
//从布局文件中获取TextView
quote=(TextView)findViewById(R.id.textView3);
author=(TextView)findViewById(R.id.textView4);
id=1;
//获取传感器服务的挂钩
sManager=(SensorManager)getSystemService(Context.SENSOR_服务);
if(sManager.getSensorList(Sensor.TYPE_陀螺仪).size()!=0){
传感器s=sManager.getSensorList(传感器.TYPE_陀螺仪).get(o);
sManager.registerListener(这是,s,SensorManager.SENSOR\u DELAY\u NORMAL);
getQuote(id,s1,s2);
quote.setText(s1);
作者:setText(s2);
}
}
//此活动何时开始
@凌驾
受保护的void onResume()
{
super.onResume();
/*注册传感器侦听器以侦听陀螺仪传感器,使用
此类中定义的回调,并尽快收集传感器信息
尽可能*/
sManager.registerListener(这是sManager.getDefaultSensor(Sensor.TYPE\u陀螺仪)、SensorManager.Sensor\u DELAY\u faster);
}
//当此活动不再可见时
@凌驾
受保护的void onStop()
{
//取消注册传感器侦听器
sManager.unregisterListener(此);
super.onStop();
}
@凌驾
已更改准确性上的公共无效信息(传感器arg0、内部arg1)
{
//什么也不做。
}
@凌驾
传感器更改时的公共无效(传感器事件)
{
//如果传感器不可靠,则返回空隙
if(event.accurity==SensorManager.SENSOR\u状态\u不可靠)
{
返回;
}
//否则它将输出横摇、俯仰和哈欠值
x=事件值[2];
total=quotedatabase.getQuotesCount();
如果(x>25){
如果(id==总数)
{id=1;
}
否则{
id++;
}
getQuote(id,s1,s2);
quote.setText(s1);
作者:setText(s2);
}

如果(x此错误表示光标中没有数据。您的数据库中有数据吗?您还应该检查moveToFirst的结果,因为假值将显示光标是否为空。

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


此外,执行此代码也没有任何效果。在本例中,您为methode提供了一个字符串。您方法中的S1与您为方法提供的字符串不同。通过向方法提供变量,JAVA创建了此变量的副本。

如果这是真的,他将获得一个游标AutofBound:Index 2请求,大小为2或类似于在,但我同意关于它对代码没有影响的部分,尽管应该明确的是,它创建了一个基本类型的副本,而不是一个对象(字符串作为基本类型)
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();
}