2017-11-30 2 views
0

특정 멤버의 id을 사용하여 데이터베이스를 사용하여 로그인하려고합니다. 그러나 로그 오류와 함께 응용 프로그램이 충돌합니다. 여러 다른 유사한 오류가 발생하여 "c1.movetoFirst();" 와 "c2.movetoFirst();"가 있습니다. 하지만 여전히 작동하지 않습니다.Android sqlite : 메서드 onclick을 실행할 수 없습니다.

로그 캣 오류는 다음과 같습니다

FATAL EXCEPTION: main 
Process: no.nordicsemi.android.nrftoolbox, PID: 13196 
java.lang.IllegalStateException: Could not execute method for android:onClick 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 
at android.view.View.performClick(View.java:4785) 
at android.view.View$PerformClick.run(View.java:19888) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5276) 
                 at java.lang.reflect.Method.invoke(Native Method) 
                 at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706) 
Caused by: java.lang.reflect.InvocationTargetException 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick 
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:432) 
at android.database.AbstractWindowedCursor.checkPosition 
at android.database.AbstractWindowedCursor.getInt 
at no.nordicsemi.android.nrftoolbox.LoginActivity.login 

LoginActivity은 다음과 같습니다

public class LoginActivity extends AppCompatActivity { 

    TextView email; 
    TextView pass; 


    private no.nordicsemi.android.nrftoolbox.myDbAdapter mydb; 

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     email = (TextView) findViewById(R.id.editText_mail); 
     pass = (TextView) findViewById(R.id.editText_pass); 
     mydb = new no.nordicsemi.android.nrftoolbox.myDbAdapter(this); 

    } 

    public void register(View v) { 
     Intent goToSecond = new Intent(); 
     goToSecond.setClass(this, Register_Page.class); 
     startActivity(goToSecond); 
    } 

    public void login(View v) { 

     Cursor c1 = mydb.getEmail(email); 
     c1.moveToFirst(); 
     Cursor c2 = mydb.getpass(pass); 
     c2.moveToFirst(); 
     int id1=c1.getInt(0); 
     int id2=c2.getInt(0); 

     if (id1>0 & id2>0) { 
      Intent goToSecond = new Intent(); 
      goToSecond.setClass(this, Profile.class); 
      startActivity(goToSecond); 
     } else 
      message(getApplicationContext(), "not valid user"); 
    } 
} 

myDbHelper은 다음과 같습니다

public class myDbAdapter extends SQLiteOpenHelper { 
    private static final int DATABASE_VERSION = 4; 
    public static final String DATABASE_NAME = "MyDBName.db"; 
    public static final String CONTACTS_TABLE_NAME = "contacts"; 
    public static final String CONTACTS_COLUMN_ID = "id"; 
    public static final String CONTACTS_COLUMN_NAME = "name"; 
    public static final String CONTACTS_COLUMN_EMAIL = "email"; 
    public static final String CONTACTS_COLUMN_PASS = "password"; 
    public static final String CONTACTS_COLUMN_DOB = "dateofbirth"; 
    public static final String CONTACTS_COLUMN_GENDER = "gender"; 
    public static final String CONTACTS_COLUMN_PHONE="phone"; 
    public static final String CONTACTS_COLUMN_CITY="city"; 
    public static final String CONTACTS_COLUMN_WALLET="wallet"; 
    private HashMap hp; 

    public myDbAdapter(Context context) { 
     super(context, DATABASE_NAME , null, 4); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL(
       "CREATE TABLE " + CONTACTS_TABLE_NAME + "(" + CONTACTS_COLUMN_ID + " INTEGER PRIMARY KEY," + CONTACTS_COLUMN_NAME + " TEXT," + CONTACTS_COLUMN_EMAIL + " TEXT," + CONTACTS_COLUMN_PASS +" TEXT," + CONTACTS_COLUMN_DOB + " TEXT," + CONTACTS_COLUMN_GENDER + " TEXT," + CONTACTS_COLUMN_PHONE + " INTEGER," + CONTACTS_COLUMN_CITY + " TEXT,"+CONTACTS_COLUMN_WALLET + " INTEGER DEFAULT 0);" 
     ); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS contacts"); 
     onCreate(db); 
    } 

    public boolean insertContact (String name, String email, String pass, String dob, String gender, String phone, String city) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put("name", name); 
     contentValues.put("email", email); 
     contentValues.put("password", pass); 
     contentValues.put("dateofbirth", dob); 
     contentValues.put("gender", gender); 
     contentValues.put("phone", phone); 
     contentValues.put("city", city); 
     db.insert("contacts", null, contentValues); 
     return true; 
    } 
    //Cursor csr = no.nordicsemi.android.nrftoolbox.CommonSQLiteUtilities.getAllRowsFromTable(db,"contacts",true‌​,null) 
    //no.nordicsemi.android.nrftoolbox.CommonSQLiteUtilities.LogCursorData(csr); 
     public int updateWallet(String amount,Integer id) 
    { 
     SQLiteDatabase db=this.getWritableDatabase(); 

     ContentValues contentValues=new ContentValues(); 
     contentValues.put("wallet",amount); 
     return(db.update("contacts",contentValues,"id = ? ",new String[] { Integer.toString(id)})); 

    } 



    public Cursor getData(int id) { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor res = db.rawQuery("select * from contacts where id="+id+"", null); 

     return res; 
    } 

    public Cursor getEmail(TextView email) 
    { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor= db.rawQuery("select id from contacts where email='"+email.getText().toString()+"'",null); 
     return cursor; 
    } 
    public Cursor getpass(TextView pass) 
    { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor= db.rawQuery("select id from contacts where password='"+pass.getText().toString()+"'",null); 
     return cursor; 
    } 
} 

경우 오류가 무엇입니까? 그리고 어떻게 그것을 다시 만들 수 있습니까?

+0

어디에서'click' 메소드를 설정합니까? – Panther

+0

커서가 비어 있기 때문에 indexoutofboundsexpception이 발생합니다. 귀하의 rawQuery를 확인하십시오 – Bek

+0

로그인 버튼의 onClick으로 "로그인"메소드를 설정했습니다. @ 팬터 –

답변

1

당신은 더 나은 cursor

Cursor c1 = mydb.getEmail(email); 
    if(c1.getcount() > 0) 
     c1.moveToFirst(); 

같이 movetoFirst를 호출하기 전에 다음이 있는지 여부를 확인하고 확인, 이메일 ID가 이미 데이터베이스에하고 대소 문자를 확인합니다.

+0

으로 확인할 수 있습니다. 대소 문자를 구분하는 데 문제가있었습니다. 감사합니다. @ rajan ks –

관련 문제