0

这是我从数据库中读取数据的代码。在这里,我只是接收值并使用“Toast”类打印它们。

public void onClickLogin(View btnLogin)
{
    String email,password;
    String[] cols = new String[] {DataBase_Server.EMAIL, DataBase_Server.PASS};
    EditText edtPassword = (EditText) findViewById(R.id.edtPassword);
    EditText edtLoginId = (EditText) findViewById(R.id.btnLogin);
    DataBase_Server database=new DataBase_Server(login.this);
    SQLiteDatabase db=database.getReadableDatabase();
    Cursor c=db.query(DataBase_Server.TABLE, null, null, null, null, null, null);
    //startManagingCursor(c);
    c.moveToFirst();
    email=c.getString(c.getColumnIndex(DataBase_Server.EMAIL));
    password=c.getString(c.getColumnIndex(DataBase_Server.PASS));
    Toast.makeText(this,email+" hello "+password, Toast.LENGTH_SHORT).show();
}

代码中没有语法错误,但是当我运行我的应用程序并单击我的登录按钮时,我得到一个强制关闭对话框,最终退出我的应用程序,并且在“logcat”中我看到一些异常,如“无法打开堆栈文件......访问被拒绝”等等等等。

日志猫:

09-20 01:28:55.524: E/AndroidRuntime(378): java.lang.IllegalStateException: Could not execute method of the activity
09-20 01:28:55.524: E/AndroidRuntime(378):  at android.view.View$1.onClick(View.java:2031)
09-20 01:28:55.524: E/AndroidRuntime(378):  at android.view.View.performClick(View.java:2364)
09-20 01:28:55.564: E/dalvikvm(378): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
09-20 01:45:58.265: E/AndroidRuntime(408): Uncaught handler: thread main exiting due to uncaught exception
09-20 01:45:58.295: E/AndroidRuntime(408): java.lang.IllegalStateException: Could not execute method of the activity
09-20 01:45:58.295: E/AndroidRuntime(408):  at android.view.View$1.onClick(View.java:2031)
09-20 01:45:58.295: E/AndroidRuntime(408):  at android.view.View.performClick(View.java:2364)
09-20 01:45:58.295: E/AndroidRuntime(408):  at android.view.View.onTouchEvent(View.java:4179)
09-20 01:45:58.295: E/AndroidRuntime(408):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
09-20 01:45:58.295: E/AndroidRuntime(408):  at android.view.View.dispatchTouchEvent(View.java:3709)
09-20 01:45:58.295: E/AndroidRuntime(408):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
  09-20 01:45:58.295: E/AndroidRuntime(408):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    09-20 01:45:58.295: E/AndroidRuntime(408):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
     09-20 01:45:58.295: E/AndroidRuntime(408):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
      09-20 01:45:58.295: E/AndroidRuntime(408):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
    09-20 01:45:58.295: E/AndroidRuntime(408):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
     09-20 01:45:58.295: E/AndroidRuntime(408):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
      09-20 01:45:58.295: E/AndroidRuntime(408):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
    09-20 01:45:58.295: E/AndroidRuntime(408):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
     09-20 01:45:58.295: E/AndroidRuntime(408):     at android.os.Handler.dispatchMessage(Handler.java:99)
    09-20 01:45:58.295: E/AndroidRuntime(408):  at android.os.Looper.loop(Looper.java:123)
    09-20 01:45:58.295: E/AndroidRuntime(408):  at android.app.ActivityThread.main(ActivityThread.java:4363)
   09-20 01:45:58.295: E/AndroidRuntime(408):   at java.lang.reflect.Method.invokeNative(Native Method)
    09-20 01:45:58.295: E/AndroidRuntime(408):  at java.lang.reflect.Method.invoke(Method.java:521)
  09-20 01:45:58.295: E/AndroidRuntime(408):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  09-20 01:45:58.295: E/AndroidRuntime(408):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
   09-20 01:45:58.295: E/AndroidRuntime(408):   at  dalvik.system.NativeStart.main(Native Method)
     09-20 01:45:58.295: E/AndroidRuntime(408): Caused by: java.lang.reflect.InvocationTargetException
  09-20 01:45:58.295: E/AndroidRuntime(408):    at com.example.curbrain.login.onClickLogin(login.java:36)
   09-20 01:45:58.295: E/AndroidRuntime(408):   at java.lang.reflect.Method.invokeNative(Native Method)
  09-20 01:45:58.295: E/AndroidRuntime(408):    at java.lang.reflect.Method.invoke(Method.java:521)
   09-20 01:45:58.295: E/AndroidRuntime(408):   at android.view.View$1.onClick(View.java:2026)
     09-20 01:45:58.295: E/AndroidRuntime(408):     ... 21 more
   09-20 01:45:58.295: E/AndroidRuntime(408): Caused by: java.lang.ClassCastException: android.widget.Button
   09-20 01:45:58.295: E/AndroidRuntime(408):   ... 25 more
    09-20 01:45:58.325: E/dalvikvm(408): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

XML 代码:

android:onClick="onClickLogin"
4

2 回答 2

2

您尝试引用您的 XML 登录 EditText 但您实际上使用了按钮的 ID:

 EditText edtLoginId = (EditText) findViewById(R.id.btnLogin); 

因此,您不能将返回的视图(按钮)转换为 EditText

该 ID 用于按钮:R.id.btnLogin检查您的 XML 以获取正确的 ID。

在 LogCat 中解释它的行是:

 09-20 01:45:58.295: E/AndroidRuntime(408): Caused by: java.lang.ClassCastException: android.widget.Button
于 2012-09-19T20:25:33.383 回答
1

你有一个错字。两者之间存在大小写差异:

android:onClick="onClickLogin"

和:

public void OnClickLogin(View btnLogin)

将您的方法更改为小写“o”:

public void onClickLogin(View btnLogin)

我看到您更新了问题以修复此错字,但您是否检查过它是否在您的代码中?

于 2012-09-19T20:25:10.237 回答