簡體   English   中英

你能幫我解釋為什么我會得到這個例外嗎? 請檢查下面的logcat

[英]Can you help me why I'm getting this Exception? Please check my logcat below

這是我的DataBase Manager類

public class MyDbManager {

private String DB_NAME="MyDb";
private String TABLE="TableReg";
private int DB_VERSION=1;
//private String ID="id";
private String Fname="Fname";
private String Lname="Lname";
private String Email="Email";
private String Password="Password";
private String Language="Languages";
private String Gender ="gender"; 
private String Id="id";

private SQLiteDatabase sdb;
Context context;
public MyDbManager(Context context) {
    // TODO Auto-generated constructor stub
    this.context=context;
    CustomSQLiteOpenHelper helper= new CustomSQLiteOpenHelper(context);
    this.sdb=helper.getWritableDatabase();      
}

public void insertValues(String fn,String ln, String em, String pwd)
{
    ContentValues values=new ContentValues();
    values.put(Fname, fn);
    values.put(Lname, ln);
    values.put(Email, em);
    values.put(Password, pwd);

// values.put(Language,lang); // values.put(Gender,gen);

    sdb.insert(TABLE, null, values);
}


public ArrayList<Object> getRow(String email)
{
    // create an array list to store data from the database row.
    // I would recommend creating a JavaBean compliant object 
    // to store this data instead.  That way you can ensure
    // data types are correct.
    ArrayList<Object> rowArray = new ArrayList<Object>();
    Cursor cursor;

    try
    {

        cursor = sdb.query
        (
                TABLE,
                new String[]{Id,Fname,Lname,Email,Password},
                Email + "=" + email,
                null, null, null, null, null
        );

        // move the pointer to position zero in the cursor.
        cursor.moveToFirst();

        // if there is data available after the cursor's pointer, add
        // it to the ArrayList that will be returned by the method.
        if (!cursor.isAfterLast())
        {
            do
            {
                rowArray.add(cursor.getLong(0));
                rowArray.add(cursor.getString(1));
                rowArray.add(cursor.getString(2));
                rowArray.add(cursor.getString(3));
                rowArray.add(cursor.getString(4));

            }
            while (cursor.moveToNext());
        }

        // let java know that you are through with the cursor.
        cursor.close();
    }
    catch (SQLException e) 
    {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }

    // return the ArrayList containing the given row from the database.
    return rowArray;
}
private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
    public CustomSQLiteOpenHelper(Context context)
    {
        super(context,DB_NAME,null,DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {

        String newTableQueryString = " create table " + 
                                      TABLE + 
                                      " (" +
                                       Id + " integer primary key autoincrement not null," +
                                      Fname + " text," + 
                                      Lname + " text," + 
                                      Email + " text," + 
                                      Password + " text" +
                                      ");";


        db.execSQL(newTableQueryString);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        //
    }
}

}

我嘗試檢索行的Activity:

public class Login extends Activity {

MyDbManager dbm;
EditText ed_em,ed_ps;
Button btn_sgn_in;
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    dbm=new MyDbManager(this);
    setupviews();
    btn_sgn_in.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            retrieveRow();
        }
    });

}

public void retrieveRow()
{
    String Email=ed_em.getText().toString();
    ArrayList<Object> row;
    row=dbm.getRow(Email);
    String r1=(String)row.get(3);
    Toast.makeText(getBaseContext(), r1, Toast.LENGTH_LONG).show();

}
public void setupviews()
{
    ed_em=(EditText)findViewById(R.id.ed_email_ver);
    ed_ps=(EditText)findViewById(R.id.ed_pass_ver);
    btn_sgn_in=(Button)findViewById(R.id.btn_sgn_in);
}

}

我試圖從數據庫中檢索一行,我收到錯誤..請幫助我。 我是Android的新手。 在此輸入圖像描述

它表示您正在調用ArrayList的第三個元素(index = 3),但是ArrayList的大小為0,因此您不受約束,您可能需要確保在ArrayList添加了數據以避免這種情況一種異常,或在你做任何功能之前檢查ArrayList是否為空。 祝好運。

在If條件下首先檢查arraylist的大小

if(arraylist.size()>3)
{
     //use arraylist here
}

並將dbhelper中的方法更改為

public ArrayList<Object> getRow(String email)
{
    // create an array list to store data from the database row.
    // I would recommend creating a JavaBean compliant object 
    // to store this data instead.  That way you can ensure
    // data types are correct.
    ArrayList<Object> rowArray = new ArrayList<Object>();
    Cursor cursor;

    try
    {
        Cursor cursor = this.db.query(TABLE, new String[] { "Id,Fname,Lname,Email,Password" },
                "Email =  \"" + email +"\" " , null, null, null,null);

        if (cursor.moveToFirst()) {
             do {
                    rowArray.add(cursor.getLong(0));
                    rowArray.add(cursor.getString(1));
                    rowArray.add(cursor.getString(2));
                    rowArray.add(cursor.getString(3));
                    rowArray.add(cursor.getString(4));              


               } while (cursor.moveToNext());
          }
          if (cursor != null && !cursor.isClosed()) {
             cursor.close();
          }
    }
    catch (SQLException e) 
    {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }

    // return the ArrayList containing the given row from the database.
    return rowArray;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM