我的android编程有问题。 我有三个课程。
Main2_Activity Class
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);
Estekhare Class
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 Class
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).
答案 0 :(得分:2)
如果列不存在,则cursor.getColumnIndex将返回-1。
我开始检查以确保您的Sheet1表包含您尝试访问的所有列,并且确实按照您在代码中指定的方式命名它们。否则,当您尝试从光标获取-1的列数据时,您将获得IllegalStateException。