2016-09-13 81 views
0

我有android編程的問題。 我有三個類。java.lang.IllegalStateException:無法讀取0行,列-1

public class Main2_Activity extends Activity{ 
ArrayList<Estekhare> estekhareHa = new ArrayList<Estekhare>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final TextView txt_soore = (TextView) findViewById(R.id.txt_soore); 
    final TextView txt_aye = (TextView) findViewById(R.id.txt_aye); 
    final TextView txt_safhe = (TextView) findViewById(R.id.txt_safhe); 
    final TextView txt_koli = (TextView) findViewById(R.id.txt_koli); 
    final TextView txt_moamele = (TextView) findViewById(R.id.txt_moamele); 
    final TextView txt_ezdevaj = (TextView) findViewById(R.id.txt_ezdevaj); 
    final TextView txt_natije = (TextView) findViewById(R.id.txt_natije); 
    final TextView txt_main_aye = (TextView) findViewById(R.id.txt_main_aye); 
    final TextView txt_translate = (TextView) findViewById(R.id.txt_translate); 

    Button btnAgain=(Button) findViewById(R.id.btnAgain); 

    Cursor cursor = G.DB.rawQuery("SELECT * FROM Sheet1", null); 

    while (cursor.moveToNext()) { 
     Estekhare estekhare = new Estekhare(); 
     estekhare.natije = cursor.getString(cursor.getColumnIndex("natije")); 
     estekhare.koli = cursor.getString(cursor.getColumnIndex("koli")); 
     estekhare.ezdevaj = cursor.getString(cursor.getColumnIndex("ezdevaj")); 
     estekhare.moamele = cursor.getString(cursor.getColumnIndex("moamele")); 
     estekhare.soore = cursor.getString(cursor.getColumnIndex("soore")); 
     estekhare.aye = cursor.getString(cursor.getColumnIndex("aye")); 
     estekhare.safhe = cursor.getString(cursor.getColumnIndex("safhe")); 

     estekhare.main_translate = cursor.getString(cursor.getColumnIndex("main_translate")); 

     estekhareHa.add(estekhare); 
    } 
    cursor.close(); 

    Random rand = new Random(); 
    int n = rand.nextInt(estekhareHa.size()); 
    Estekhare estekhare = estekhareHa.get(n); 
    txt_soore.setText(estekhare.soore); 
    txt_aye.setText(estekhare.aye); 
    txt_safhe.setText(estekhare.safhe); 
    txt_koli.setText(estekhare.koli); 
    txt_moamele.setText(estekhare.moamele); 
    txt_ezdevaj.setText(estekhare.ezdevaj); 
    txt_natije.setText(estekhare.natije); 
    txt_main_aye.setText(estekhare.main_aye); 
    txt_translate.setText(estekhare.main_translate); 

public class Estekhare {
public String natije;
public String koli; public String ezdevaj; public String moamele; public String soore; public String aye; public String safhe; public String main_aye; public String main_translate; }

`公共類別G延伸的應用{ 公共靜態SQLiteDatabase DB; public static Context context; public static final String DIR_SDCARD = Environment.getExternalStorageDirectory()。getAbsolutePath(); public static String DIR_DATABASE; private final String dbName =「natije_estekhare.sqlite」;

@Override 
public void onCreate() { 
    super.onCreate(); 
    context=getApplicationContext(); 
    DIR_DATABASE= DIR_SDCARD +"/Android/data/"+ context.getPackageName()+ "/database/"; 
    new File(DIR_DATABASE).mkdirs(); 
    prepareDB(); 
} 
public boolean prepareDB() { 
    try { 
     String state = Environment.getExternalStorageState(); 
     InputStream is = G.context.getAssets().open(dbName); 

     if (state.equals(Environment.MEDIA_MOUNTED)) { 
      File DB_PATH = new File(DIR_DATABASE + "/" + dbName); 
      logger("SD MOUNTED"); 
      if (DB_PATH.exists()) { 
       logger("DB exist"); 
       DB = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null); 
       return true; 
      } 
      else { 
       logger("DB NOT exist"); 
       if (copy(new FileOutputStream(DB_PATH), is)) { 
        logger("Copy Success"); 
        DB = SQLiteDatabase.openOrCreateDatabase(DB_PATH, null); 
        return true; 
       } 
       else { 
        logger("Copy Faild"); 
        return false; 
       } 
      } 
     } 
     else { 
      logger("SD NOT MOUNTED"); 
      DIR_DATABASE = getFilesDir().toString(); 
      File dbFile = new File(getFilesDir().toString() + "/" + dbName); 
      if (dbFile.exists()) { 
       logger("DB exist"); 
       DB = SQLiteDatabase.openOrCreateDatabase(dbFile, null); 
       return true; 
      } 
      else { 
       logger("DB NOT exist"); 
       FileOutputStream os = openFileOutput(dbName, context.MODE_PRIVATE); 
       if (copy(os, is)) { 
        logger("Copy Success"); 
        DB = SQLiteDatabase.openOrCreateDatabase(dbFile, null); 
        return true; 
       } 
       else { 
        logger("Copy Faild"); 
        return false; 
       } 
      } 
     } 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

public boolean copy(OutputStream os, InputStream is) { 
    try { 
     int readed = 0; 
     byte[] buffer = new byte[8 * 1024]; 
     while ((readed = is.read(buffer)) > 0) { 
      os.write(buffer, 0, readed); 
     } 
     try { 
      is.close(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     try { 
      os.flush(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     try { 
      os.close(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return true; 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

public void logger(String Message){ 
    Log.i("LOG", Message); 
}}` 

當我運行這個程序時,它崩潰了。如何解決問題?

java.lang.RuntimeException: Unable to start activity ComponentInfo{amir.badiei.app.estekhareapp/amir.badiei.app.estekhareapp.Main2_Activity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.at amir.badiei.app.estekhareapp.Main2_Activity.onCreate(Main2_Activity.java:47). 
+0

重新格式化您的代碼片段,它不太可讀! –

回答

2

如果列不存在,那麼cursor.getColumnIndex將返回-1。

我會開始檢查以確保您的Sheet1表具有您嘗試訪問的所有列,並且它們確實按照您在代碼中指定的方式命名。否則,當你試圖從-1的列中獲得遊標的數據時,你會得到IllegalStateException。

相關問題