我在Android中遇到SQLite问题。 Eclipse不报告sintax中的任何错误,但是当在这个应用程序中调用OnClickListener时,我有:
应用程序意外停止
这是OnClickListener代码:
public void onClick(View v) {
EditText usernameTxt = (EditText) findViewById(R.id.nameTxt);
String username = usernameTxt.getText().toString();
SQL users = new SQL(getApplicationContext(), "loopr", null, 1);
SQLiteDatabase db = users.getWritableDatabase();
if (db != null){
db.execSQL("INSERT INTO users(name) VALUES ('"+username+"')");
}
}
这是SQL.java代码:
public class SQL extends SQLiteOpenHelper {
String sqlCreate = "CREATE TABLE users (name TEXT)";
public SQL(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(sqlCreate);
}
@Override
public void onUpgrade(SQLiteDatabase db, int before, int after) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS users");
}
}
问题出在哪里?我是Android的初学者,我需要帮助。谢谢:))
编辑1: LogCat:
02-17 20:33:16.717: D/ddm-heap(22023): Got feature list request
02-17 20:34:53.307: W/IInputConnectionWrapper(22023): showStatusIcon on inactive InputConnection
02-17 20:35:52.857: W/IInputConnectionWrapper(22215): showStatusIcon on inactive InputConnection
02-17 20:53:01.747: D/AndroidRuntime(22695): Shutting down VM
02-17 20:53:01.747: W/dalvikvm(22695): threadid=3: thread exiting with uncaught exception (group=0x4001e160)
02-17 20:53:01.747: E/AndroidRuntime(22695): Uncaught handler: thread main exiting due to uncaught exception
02-17 20:53:01.767: E/AndroidRuntime(22695): java.lang.ClassCastException: android.widget.TextView
02-17 20:53:01.767: E/AndroidRuntime(22695): at com.loopr.LooprActivity$1.onClick(LooprActivity.java:43)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.view.View.performClick(View.java:2397)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.view.View.onTouchEvent(View.java:4212)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.widget.TextView.onTouchEvent(TextView.java:6616)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.view.View.dispatchTouchEvent(View.java:3742)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:981)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:981)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:981)
02-17 20:53:01.767: E/AndroidRuntime(22695): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
02-17 20:53:01.767: E/AndroidRuntime(22695): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.app.Activity.dispatchTouchEvent(Activity.java:2064)
02-17 20:53:01.767: E/AndroidRuntime(22695): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.os.Looper.loop(Looper.java:123)
02-17 20:53:01.767: E/AndroidRuntime(22695): at android.app.ActivityThread.main(ActivityThread.java:4370)
02-17 20:53:01.767: E/AndroidRuntime(22695): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 20:53:01.767: E/AndroidRuntime(22695): at java.lang.reflect.Method.invoke(Method.java:521)
02-17 20:53:01.767: E/AndroidRuntime(22695): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-17 20:53:01.767: E/AndroidRuntime(22695): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-17 20:53:01.767: E/AndroidRuntime(22695): at dalvik.system.NativeStart.main(Native Method)
02-17 20:53:01.777: E/SemcCheckin(22695): Get crash dump level : java.io.FileNotFoundException: /data/semc-checkin/crashdump
02-17 20:53:01.787: I/dalvikvm(22695): threadid=7: reacting to signal 3
02-17 20:53:01.797: I/dalvikvm(22695): Wrote stack trace to '/data/anr/traces.txt'
02-17 20:53:03.437: I/Process(22695): Sending signal. PID: 22695 SIG: 9
02-17 20:56:08.947: D/AndroidRuntime(22861): Shutting down VM
02-17 20:56:08.957: W/dalvikvm(22861): threadid=3: thread exiting with uncaught exception (group=0x4001e160)
02-17 20:56:08.957: E/AndroidRuntime(22861): Uncaught handler: thread main exiting due to uncaught exception
02-17 20:56:08.977: E/AndroidRuntime(22861): java.lang.ClassCastException: android.widget.TextView
02-17 20:56:08.977: E/AndroidRuntime(22861): at com.loopr.LooprActivity$1.onClick(LooprActivity.java:43)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.view.View.performClick(View.java:2397)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.view.View.onTouchEvent(View.java:4212)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.widget.TextView.onTouchEvent(TextView.java:6616)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.view.View.dispatchTouchEvent(View.java:3742)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:981)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:981)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:981)
02-17 20:56:08.977: E/AndroidRuntime(22861): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
02-17 20:56:08.977: E/AndroidRuntime(22861): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.app.Activity.dispatchTouchEvent(Activity.java:2064)
02-17 20:56:08.977: E/AndroidRuntime(22861): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.os.Looper.loop(Looper.java:123)
02-17 20:56:08.977: E/AndroidRuntime(22861): at android.app.ActivityThread.main(ActivityThread.java:4370)
02-17 20:56:08.977: E/AndroidRuntime(22861): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 20:56:08.977: E/AndroidRuntime(22861): at java.lang.reflect.Method.invoke(Method.java:521)
02-17 20:56:08.977: E/AndroidRuntime(22861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-17 20:56:08.977: E/AndroidRuntime(22861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-17 20:56:08.977: E/AndroidRuntime(22861): at dalvik.system.NativeStart.main(Native Method)
02-17 20:56:08.997: E/SemcCheckin(22861): Get crash dump level : java.io.FileNotFoundException: /data/semc-checkin/crashdump
02-17 20:56:09.117: I/dalvikvm(22861): threadid=7: reacting to signal 3
02-17 20:56:09.127: I/dalvikvm(22861): Wrote stack trace to '/data/anr/traces.txt'
02-17 20:56:31.077: D/AndroidRuntime(23027): Shutting down VM
02-17 20:56:31.077: W/dalvikvm(23027): threadid=3: thread exiting with uncaught exception (group=0x4001e160)
02-17 20:56:31.077: E/AndroidRuntime(23027): Uncaught handler: thread main exiting due to uncaught exception
02-17 20:56:31.087: E/AndroidRuntime(23027): java.lang.ClassCastException: android.widget.TextView
02-17 20:56:31.087: E/AndroidRuntime(23027): at com.loopr.LooprActivity$1.onClick(LooprActivity.java:43)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.view.View.performClick(View.java:2397)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.view.View.onTouchEvent(View.java:4212)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.widget.TextView.onTouchEvent(TextView.java:6616)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.view.View.dispatchTouchEvent(View.java:3742)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:981)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:981)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:981)
02-17 20:56:31.087: E/AndroidRuntime(23027): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
02-17 20:56:31.087: E/AndroidRuntime(23027): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.app.Activity.dispatchTouchEvent(Activity.java:2064)
02-17 20:56:31.087: E/AndroidRuntime(23027): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.os.Looper.loop(Looper.java:123)
02-17 20:56:31.087: E/AndroidRuntime(23027): at android.app.ActivityThread.main(ActivityThread.java:4370)
02-17 20:56:31.087: E/AndroidRuntime(23027): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 20:56:31.087: E/AndroidRuntime(23027): at java.lang.reflect.Method.invoke(Method.java:521)
02-17 20:56:31.087: E/AndroidRuntime(23027): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-17 20:56:31.087: E/AndroidRuntime(23027): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-17 20:56:31.087: E/AndroidRuntime(23027): at dalvik.system.NativeStart.main(Native Method)
02-17 20:56:31.097: E/SemcCheckin(23027): Get crash dump level : java.io.FileNotFoundException: /data/semc-checkin/crashdump
答案 0 :(得分:2)
它是classcast异常(java.lang.ClassCastException:android.widget.TextView 02-17 20:53:01.767 :)。似乎nameTxt是TextView,您尝试转换为EditText。
EditText usernameTxt = (EditText) findViewById(R.id.nameTxt);
在findViewById(....)
;
答案 1 :(得分:0)
试试这个, 转到菜单项目>清洁然后选择下面选择的"清洁项目" 检查相应的项目,然后选择确定按钮。