2012-02-17 90 views
0

我在Android中遇到SQLite問題。 Eclipse不中sintax報告任何錯誤,但是當OnClickListener被調用這個程序,我有這樣的:Android中的SQLite問題

應用程序意外終止

這是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 
+1

在Eclipse中使用'adb logcat',DDMS或LogCat視圖來檢查LogCat並查看與運行時錯誤相關的堆棧跟蹤。另外,不要在這裏使用'getApplicationContext()' - 這個''會工作得很好。 – CommonsWare 2012-02-17 20:15:03

+1

用'Logcat'中的'Exception'發佈堆棧跟蹤。 – Luksprog 2012-02-17 20:15:17

+0

@CommonsWare我使用Eclipse中出現的LogCat日誌編輯了帖子:)抱歉,日誌格式不正確。 – user1217055 2012-02-17 20:29:15

回答

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(....)裏面使用有效的EditText;

+1

或者,您的資源ID可能與您的其他代碼有點不同步。在Eclipse中,從主菜單嘗試項目>清理。 – CommonsWare 2012-02-17 20:52:41

+0

謝謝@thinksteep,那就是問題! – user1217055 2012-02-17 21:40:21

+0

沒問題。享受編碼。 – kosa 2012-02-17 21:41:03

0

試試這個, 轉到菜單項目>清理,然後選擇「下面選擇的清潔項目」 檢查它相應的項目,然後選擇確定按鈕。