بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]
پرسش و پاسخ

خطا CursorIndexOutOfBoundsException

مدیریت گفتگو
khajavi 11 سال پیش
آخرین تغییر: 11 سال پیش

سلام به همه .یه سوال داشتم ممنون میشم جواب بدید. در لیست فهرست برنامم وقتی که روی علاقه مندی ها کلیک می کنی بدرستی اونو وارد لیست علاقه مندی ها می کنه اما زمانی که وارد لیست علاقه مندیها میشی و اونو کلیک میکنی بجای اینکه تو رو راهنمایی کنه به متن داستان کرش می کنه و این خطاها رو میده:

 
07-14 23:08:42.665: E/AndroidRuntime(1450): java.lang.RuntimeException: Unable to start activity ComponentInfo{book/book.matn_asli_dastan}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
07-14 23:08:42.665: E/AndroidRuntime(1450): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
07-14 23:08:42.665: E/AndroidRuntime(1450): 	at book.database.namayesh_matn(database.java:135)
07-14 23:08:42.665: E/AndroidRuntime(1450): 	at book.matn_asli_dastan.loader(matn_asli_dastan.java:110)
07-14 23:08:42.665: E/AndroidRuntime(1450): 	at book.matn_asli_dastan.onCreate(matn_asli_dastan.java:74)

اینم سورس کدهایی که خطا میگیره:

 
  
    public String namayesh_matn(String table, String season, String name) {
        Cursor Cursor = mydb.rawQuery("SELECT * FROM " + table + " where seasone='" + season + "' and name = '" + name + "'", null);
        Cursor.moveToFirst();
        String save = Cursor.getString(2);
        return save;

    }

---------------------

  private void loader(String season, String Name) {

        db.open();
        txt_title.setText(name);
        txt_text.setText(db.namayesh_matn("contents", season, Name));
        db.close();

    }

--------------------------

   loader(season, name);

ببخشید این سوال رو می پرسم اما من تقریبا تمام سوالهای اینجا و استک اور فلو و... گشتم اما نتونستم ایراد برنامه رو پیدا کنم.

نظرات

Spirit 11 سال پیش
الان این CursorIndexOutOfBoundsException یعنی Query که زدید نتیجه ای نداشته، خالی از هر گونه ردیف هست. خارج از برنامه Query تون رو تستی بکنید.
khajavi 11 سال پیش
چطوری خارج از برنامه تست کنم
Spirit 11 سال پیش
دیتابیستون رو با چی ساختید؟ تو همون میتونید Query بزنید و نتیجش رو ببینید، حالا اگر نتیجه داشت باید بیایید داخل برنامه مقادیری که به Query تون میدید رو بررسی کنید، کار سختی نیست و بهترین نفر برای حل مشکل خودتونید :)
khajavi 11 سال پیش
من دیتابیس رو با SQLite Expert Professional ساختم.یعنی Query رو باید داخل اون تست کنم؟
khajavi 11 سال پیش
من در sq lite رکورد استار رو دادم و با توجه به همون qury رو گرفتم اما در نشان دادن فصول و متون مشکلی نیست فقط در برگشت از لیست استار به متون کرش دارم
khajavi 11 سال پیش
کسی می تونه بگه چکار کنم
khajavi 11 سال پیش
بچه خواهشا یه نگاهی به کد ها بندازید ممنون

پاسخ‌ها 2

khajavi 11 سال پیش

ببخشید کسی می تونه این کد ها رو نگاه کنه یا اگه سوالم اشکال داره راهنمایی کنه

نظرات

khajavi 11 سال پیش

این سورس صفحه متن اصلی داستان

  import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class matn_asli_dastan extends Activity {

    private database  db;
    private TextView  txt_text;
    private TextView  txt_title;
    private String    season;
    private String    name;
    private ImageView txtSher;


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

        db = new database(this);
        txt_text = (TextView) findViewById(R.id.txt_text);
        txt_title = (TextView) findViewById(R.id.txt_title);

        Bundle ex = getIntent().getExtras();
        season = ex.getString("seasone");
        name = ex.getString("name");

        txt_text.setTypeface(BookActivity.font);
        txt_text.setTextSize(BookActivity.size);
        txt_text.setLineSpacing(BookActivity.space, 1);

        Typeface titr = Typeface.createFromAsset(getAssets(), "font/titr.ttf");
        txt_title.setTypeface(titr);
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.main_txt_layout);

        if (BookActivity.mood.equals("day")) {

            Resources res = getResources();
            Drawable dr = res.getDrawable(R.drawable.light);
            rl.setBackgroundDrawable(dr);
            txt_text.setTextColor(Color.BLACK);
            txt_title.setTextColor(Color.BLACK);
        } else {

            Resources res = getResources();
            Drawable dr = res.getDrawable(R.drawable.dark);
            rl.setBackgroundDrawable(dr);
            txt_text.setTextColor(Color.WHITE);
            txt_title.setTextColor(Color.WHITE);
        }

        txtSher = (ImageView) findViewById(R.id.txtSher);
        txtSher.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("text/plain");
                share.putExtra(Intent.EXTRA_TEXT, txt_text.getText());
                startActivity(Intent.createChooser(share, "اشتراک بگذارید. . . :"));
            }
        });

        loader(season, name);

    }


    private void loader(String season, String Name) {

        db.open();
        txt_title.setText(name);
        txt_text.setText(db.namayesh_matn("contents", season, Name));
        db.close();

    }
}

سورس xml متن اصلی

 ?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_txt_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" android:orientation="vertical">


            <ImageView
                android:id="@+id/txtSher"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/share" />


            <TextView
                android:id="@+id/txt_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="15dip"
                android:gravity="center"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#000000" />


            <TextView
                android:id="@+id/txt_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:gravity="right"
                android:text="TextView"
                android:textColor="#000000" />
            
            <ir.adad.AdView
        android:id="@+id/ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        token="ef7c39fe64d5eadb2f4f3386de"/>
    <ir.dgad.AdView
        android:id="@+id/adView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

اینم دیتابیس

 import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class database extends SQLiteOpenHelper {

    public final String   path = "data/data/book/databases/";
    public final String   Name = "database";
    public SQLiteDatabase mydb;
    private final Context mycontext;


    public database(Context context) {
        super(context, "database", null, 1);
        mycontext = context;
    }


    @Override
    public void onCreate(SQLiteDatabase arg0) {}


    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {}


    public void useable() {
        boolean checkdb = checkdb();
        if (checkdb) {

        } else {
            this.getReadableDatabase();
            try {
                copydatabase();
            }
            catch (IOException e) {}
        }
    }


    public void open() {
        mydb = SQLiteDatabase.openDatabase(path + Name, null, SQLiteDatabase.OPEN_READWRITE);
    }


    @Override
    public void close() {
        mydb.close();
    }


    public boolean checkdb() {
        SQLiteDatabase db = null;
        try {
            db = SQLiteDatabase.openDatabase(path + Name, null, SQLiteDatabase.OPEN_READONLY);
        }
        catch (SQLException e) {}
        return db != null ? true : false;
    }


    public void copydatabase() throws IOException {
        OutputStream myoutput = new FileOutputStream(path + Name);
        byte[] buffer = new byte[1024];
        int lengh;
        InputStream myInput = mycontext.getAssets().open(Name);
        while ((lengh = myInput.read(buffer)) > 0) {
            myoutput.write(buffer, 0, lengh);
        }
        myInput.close();
        myoutput.flush();
        myInput.close();
    }


    public String namayesh(int row, int field, String table) {

        Cursor Cursor = mydb.rawQuery("SELECT * FROM " + table, null);
        Cursor.moveToPosition(row);
        String str = Cursor.getString(field);
        return str;
    }


    public Integer shomaresh_field(String table, String field) {
        Cursor Cursor = mydb.rawQuery("SELECT * FROM " + table + " group by " + field, null);
        int i = Cursor.getCount();
        return i;
    }


    public String namayesh_fasl(String table, int row) {
        Cursor Cursor = mydb.rawQuery("SELECT * FROM " + table + " group by seasone ", null);
        Cursor.moveToPosition(row);
        String s = Cursor.getString(3);
        return s;
    }


    public Integer shomaresh_dastan(String table, String season) {
        Cursor Cursor = mydb.rawQuery("SELECT * FROM " + table + " where seasone='" + season + "'  group by name", null);
        int s = Cursor.getCount();
        return s;
    }


    public String namayesh_dastan(String table, int row, String season, int field) {
        Cursor Cursor = mydb.rawQuery("SELECT * FROM " + table + " where seasone='" + season + "' group by name", null);
        Cursor.moveToPosition(row);
        String save = Cursor.getString(field);
        return save;
    }


    public Integer shomaresh_safhe_dastan(String table, String season, String story) {
        Cursor Cursor = mydb.rawQuery("SELECT * FROM " + table + " where seasone='" + season + "' and name = '" + story + "'", null);
        int save = Cursor.getCount();
        return save;
    }


    public String namayesh_matn(String table, String season, String name) {
        Cursor Cursor = mydb.rawQuery("SELECT * FROM " + table + " where seasone='" + season + "' and name = '" + name + "'", null);
        Cursor.moveToFirst();
        String save = Cursor.getString(2);
        return save;

    }


    public String jostojoo(int row, int col, String word, String field) {
        Cursor cursor;
        if (field.equals("name")) {
            cursor = mydb.rawQuery("select * from contents where " + field + " like '%" + word + "%' group by name", null);
        } else {
            cursor = mydb.rawQuery("select * from contents where " + field + " like '%" + word + "%'", null);
        }
        cursor.moveToPosition(row);
        String save = cursor.getString(col);
        return save;
    }


    public Integer shmaresh_jostojoo(String word, String field) {
        Cursor cursor;
        if (field.equals("name")) {
            cursor = mydb.rawQuery("select * from contents where " + field + " like '%" + word + "%' group by name", null);
        } else {
            cursor = mydb.rawQuery("select * from contents where " + field + " like '%" + word + "%'", null);
        }
        int save = cursor.getCount();
        return save;
    }


    public void broozresani_doostdashtaniha(String table, String season, String name, String value) {
        ContentValues cv = new ContentValues();

        cv.put("star", value);
        mydb.update(table, cv, "seasone='" + season + "' and name='" + name + "'", null);
    }


    public Integer shomaresh_doostdashtaniha(String table) {
        Cursor Cursor = mydb.rawQuery("select * from " + table + " where star=1 group by name", null);
        int save = Cursor.getCount();
        return save;
    }


    public String namayesh_doostdashtaniha(String table, int row, int field) {
        Cursor Cursor = mydb.rawQuery("select * from " + table + " where star=1 group by name", null);
        Cursor.moveToPosition(row);
        String save = Cursor.getString(field);
        return save;
    }
}

بازم ممنون از وقتی که میذاری

نظرات

khajavi 11 سال پیش
گذاشتم محسن جان

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .