Android sqlite:无法执行onclick方法

时间:2017-11-30 03:35:14

标签: java android sqlite

我正在尝试使用特定成员的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 使用数据库登录。 但该应用程序崩溃与日志错误。 在其他各种类似的错误后,我加入了“c1.movetoFirst();”和“c2.movetoFirst();”。但它还是不起作用。

logcat错误是:

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");
    }
}

LoginActivity是:

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

myDbHelper是:

cin >> i

错误在哪里?怎么能被重新认识呢?

1 个答案:

答案 0 :(得分:1)

中调用cursor之前,最好先检查movetoFirst是否有下一个 Cursor c1 = mydb.getEmail(email); if(c1.getcount() > 0) c1.moveToFirst();
dict= {('P06', 'P09'): 36340, ('P01', 'P05', 'P06', 'P09'): 10085, ('P01', 'P06'): 36337, ('P01', 'P09'): 49897, ('P02', 'P09'): 11573}

df = pd.DataFrame.from_dict(dict, orient='index')

df.rename_axis('Product Id').reset_index()\
  .assign(length = df.index.str.len()).rename(columns={0:'Count'})

并确保,电子邮件ID已在数据库中并检查区分大小写。