我正在尝试通过验证数据库中已有的电子邮件和密码来创建登录页面。但是,会发生致命异常。
这是我的LoginActivity.java,myDbHelper.java,activity_login.xml和logcat错误。
logcat错误如下:
FATAL EXCEPTION:main
Process:no.nordicsemi.android.nrftoolbox,PID:9052
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 java.lang.reflect.Method.invoke(
Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
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:android.database.sqlite.SQLiteException:near ".":
syntax error(code 1):,while compiling:select *
from contacts
where email = android.support.v7.widget.AppCompatEditText
{
1d 2 b15f4 VFED..CL ........ 0, 72 - 1080, 208 #7f 090085 app:
id / editText_mail
}
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(
Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at no.nordicsemi.android.nrftoolbox.myDbAdapter.getEmail(myDbAdapter.java:208)
at no.nordicsemi.android.nrftoolbox.LoginActivity.login(LoginActivity.java:41)
我的LoginActivity.java:
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);
Cursor c2 = mydb.getpass(pass);
if (c1 != null & c2 != null) {
Intent goToSecond = new Intent();
goToSecond.setClass(this, Profile.class);
startActivity(goToSecond);
} else
message(getApplicationContext(), "not valid user");
}
}
myDbHelper.java(getEmail和getpass方法)如下:
public Cursor getEmail(TextView email)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor= db.rawQuery( "select * from contacts where email="+email+"",null);
return cursor;
}
public Cursor getpass(TextView pass)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor= db.rawQuery( "select * from contacts where password="+pass+"",null);
return cursor;
}
导致应用崩溃的原因是什么? 可能的错误在哪里?
我做了建议的更改。但现在logcat错误显示:
Caused by: android.database.sqlite.SQLiteException: near "@gmail": syntax error (code 1): , while compiling: select * from contacts where email=kingervinny@gmail.com
我怎样才能纠正这个?
答案 0 :(得分:1)
EditText email = (EditText) findViewById(R.id.editText_mail);
EditText pass = (EditText) findViewById(R.id.editText_pass);
Cursor c1 = mydb.getEmail(email.getText());
Cursor c2 = mydb.getpFass(pass.getText());
AND
Cursor cursor= db.rawQuery( "select * from contacts where email='"+email.getText().toString()+"'",null);
Cursor cursor= db.rawQuery( "select * from contacts where password='"+pass.getText().toString()+"'",null);
答案 1 :(得分:0)
SELECT
声明。SQLiteException:near“。”:语法错误(代码1):,编译时:
使用 getText().toString()
。您的 SELECT
语句将为。
Cursor cursor= db.rawQuery( "select * from contacts where email='"+email.getText().toString()+"'",null);
并且
Cursor cursor= db.rawQuery( "select * from contacts where password='"+pass.getText().toString()+"'",null);
答案 2 :(得分:0)
导致应用崩溃的原因是什么?可能的错误在哪里?
导致email
和pass
问题,因为sql查询始终为字符串,而email
和pass
不是String
,而是{{1因此崩溃
将TextView
更改为:
myDbHelper.java