Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.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
java.lang.IllegalStateException:无法';t读取第0行第1列_Java_Android - Fatal编程技术网

java.lang.IllegalStateException:无法';t读取第0行第1列

java.lang.IllegalStateException:无法';t读取第0行第1列,java,android,Java,Android,我对android编程有问题。 我有三节课 Main2_活动类 public class Main2_Activity extends Activity{ ArrayList<Estekhare> estekhareHa = new ArrayList<Estekhare>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreat

我对android编程有问题。 我有三节课

Main2_活动类

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;
}
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);
    }
}
G级

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;
}
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。

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


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

重新格式化代码段,它不太可读!重新格式化您的代码片段,它不太可读!