This is my code for reading the data from my database. Here I’m just receiving the values and printing them using “Toast” class.
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();
}
There are no syntax errors in the code but when I’m running my app and click on my login button I get a force close dialog box which ultimately quits my app and in the “logcat” I see some exception like “Unable to open stack file ……access denied” etc. etc.
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 CODE :
android:onClick="onClickLogin"
Your trying to reference your XML login EditText but you’ve actually used the ID of an Button:
Therefore you cannot cast the view returned (a Button) to an EditText
That id is for a button:
R.id.btnLogincheck your XML for the correct ID.The line explaining it in your LogCat is: