gpt4 book ai didi

java.lang.IllegalStateException : Couldn't read row 0, col -1

转载 作者:行者123 更新时间:2023-12-02 03:22:05 24 4
gpt4 key购买 nike

我在 Android 编程方面遇到问题。我有三个类(class)。

Main2_Activity 类

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级

public class G extends Application{
public static 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).

最佳答案

如果列不存在,则cursor.getColumnIndex 将返回-1。

我将开始检查以确保您的 Sheet1 表包含您尝试访问的所有列,并且它们的命名确实与您在代码中指定的方式完全相同。否则,当您尝试从光标获取 -1 列的数据时,您将收到 IllegalStateException。

关于java.lang.IllegalStateException : Couldn't read row 0, col -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39476574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com