Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.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 Android sqlite:无法执行方法onclick_Java_Android_Sqlite - Fatal编程技术网

Java Android sqlite:无法执行方法onclick

Java Android sqlite:无法执行方法onclick,java,android,sqlite,Java,Android,Sqlite,我正试图使用特定成员的id使用数据库登录。 但应用程序因日志错误而崩溃。 在其他各种类似错误之后,我包括了“c1.movetoFirst();”和“c2.movetoFirst();”。但它仍然不起作用 logcat错误为: FATAL EXCEPTION: main Process: no.nordicsemi.android.nrftoolbox, PID: 13196 java.lang.IllegalStateException: Could not execute method for

我正试图使用特定成员的
id
使用数据库登录。 但应用程序因日志错误而崩溃。 在其他各种类似错误之后,我包括了“c1.movetoFirst();”和“c2.movetoFirst();”。但它仍然不起作用

logcat错误为:

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
后勤活动是:

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

错误在哪里?如何背诵呢?

在调用
movetoFirst
之前,最好检查
光标是否有next,如中所示

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

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

您在哪里设置
单击
方法?您的光标为空,这就是您获得indexoutofboundsexpception的原因。检查您的RAWQUERI是否已将“登录”方法设置为单击“登录”按钮@Panthershare您的dbhelper类完全共享,以便我们可以帮助您共享它@贝基特成功了。区分大小写有问题。非常感谢。@rajan ks